Manipulate fields

S

Sashi

All,

I have a csv file. The second field is an int and I need it to be
replaced with the two's power of itself.
For example if the number is 22, I need it replaced with two raised to
the power of 22.
The remaining fields are IP address which should be replaced with the
corresponding ints, ie use the inet_aton() funcion. There will be at
least two but no more fields per row.

I'm a Perl newbie so if someone could point me to a solution that'll
be a help.

Thanks,
Sashi
 
K

Klaus

I have a csv file. The second field is an int and I need it to be
replaced with the two's power of itself.
For example if the number is 22, I need it replaced with two raised to
the power of 22.
The remaining fields are IP address which should be replaced with the
corresponding ints, ie use the inet_aton() funcion. There will be at
least two but no more fields per row.

I'm a Perl newbie so if someone could point me to a solution that'll
be a help.

for big integers (i.e. > 2 ** 32)
use bigint;

Other than that, I would suggest you start your program with the
following two lines:

use strict;
use warnings;

This gives you plenty of warnings in case you go the wrong way.

But then again, this is only a suggestion, if you prefer not to have
"use strict" / "use warnings", your program can run without (but it
might not produce the expected result).
 
S

Sashi

None can provide a viable solution in lieu of a data example.
Provide a precise and exact example of your data.

Purl Gurl

199.67.218.64,26,199.67.218.76,199.67.218.77
199.67.236.224,28,199.67.236.225,
199.67.236.240,28,199.67.236.241,
 
A

Ala Qumsieh

Purl said:
#!perl

$input = "199.67.218.64,26,199.67.218.76,199.67.218.77";

$input =~ s/,(\d+),/,2 ** $1,/;

print $input;

print "\n\n";

$input = "199.67.218.64,26,199.67.218.76,199.67.218.77";

if ($input =~ /,(\d+),/)
{
$new = 2 ** $1;
$input =~ s/,(\d+),/,$new,/;
}

print $input;

or more succinctly:

$input =~ s/(?<=,)(\d+)(?=,)/2 ** $1/e;

--Ala
 
M

Mirco Wahab

Ala said:
or more succinctly:

$input =~ s/(?<=,)(\d+)(?=,)/2 ** $1/e;

Nice Idea, that would give "a whole"
program like:

...
s/\b([\w\.]+)\b/fmt $1/eg and print while <DATA>;
...


Where the 'fmt' function would, depending
on the format needed, look like:

...
sub fmt { ($_=shift)=~/\./ ? unpack "H*",inet_aton $_ : sprintf "0x%08X",2**$_ }
...

The above would print the following output
(if the data provided by the OP is below __DATA__)

c743da40,0x04000000,c743da4c,c743da4d
c743ece0,0x10000000,c743ece1,
c743ecf0,0x10000000,c743ecf1,


For the OP: the 'prologue' needed for this
'program' would be:

use strict;
use warnings;
use Socket;
...


Regards

M.
 
D

Dr.Ruud

Sashi schreef:
I have a csv file. The second field is an int and I need it to be
replaced with the two's power of itself.
For example if the number is 22, I need it replaced with two raised to
the power of 22.

echo 'abc,12,33' |perl -wpe'
s/^([^,]*,)([^,]*)/$1.q{0b1}.q{0}x$2/e
'
abc,0b1000000000000,33
The remaining fields are IP address which should be replaced with the
corresponding ints, ie use the inet_aton() funcion. There will be at
least two but no more fields per row.

Have you already looked into the several CSV-related modules on cpan?
 
A

Ala Qumsieh

Purl said:
(snipped confusion on the part of Ala Qumsieh)

You have responded to the wrong person. I did not
present a question to this group. You should be
responding to the author who did ask a question.

Actually I was trying to teach YOU something new, since after all those
years lurking on this newsgroup, your coding still lacks.

Btw, didn't I killfile you a while ago? how come I'm still seeing your
posts? hmmm ..

--Ala
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top