Manipulate fields

Discussion in 'Perl Misc' started by Sashi, Apr 20, 2007.

  1. Sashi

    Sashi Guest

    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
     
    Sashi, Apr 20, 2007
    #1
    1. Advertising

  2. Sashi

    Klaus Guest

    On Apr 20, 6:33 pm, Sashi <> wrote:
    > 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).

    --
    Klaus
     
    Klaus, Apr 20, 2007
    #2
    1. Advertising

  3. Sashi

    Sashi Guest

    > 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,
     
    Sashi, Apr 20, 2007
    #3
  4. Sashi

    Ala Qumsieh Guest

    Purl Gurl wrote:

    >
    > #!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
     
    Ala Qumsieh, Apr 20, 2007
    #4
  5. Sashi

    Mirco Wahab Guest

    Ala Qumsieh wrote:
    > Purl Gurl wrote:
    >> if ($input =~ /,(\d+),/)
    >> {
    >> $new = 2 ** $1;
    >> $input =~ s/,(\d+),/,$new,/;
    >> }
    >>
    >> print $input;

    >
    > 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.
     
    Mirco Wahab, Apr 20, 2007
    #5
  6. Sashi

    Dr.Ruud Guest

    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?

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Apr 22, 2007
    #6
  7. Sashi

    Ala Qumsieh Guest

    Purl Gurl wrote:

    > Ala Qumsieh wrote:
    >
    >> Purl Gurl wrote:

    >
    > (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
     
    Ala Qumsieh, Apr 23, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    1
    Views:
    332
    Mike Wahler
    Jan 22, 2005
  2. call_me_anything
    Replies:
    4
    Views:
    469
    Pete Becker
    Sep 30, 2007
  3. Cyril.Liu
    Replies:
    0
    Views:
    455
    Cyril.Liu
    Dec 2, 2008
  4. middletree

    how to copy from fields to other fields

    middletree, Nov 5, 2003, in forum: ASP General
    Replies:
    1
    Views:
    168
    middletree
    Nov 5, 2003
  5. Replies:
    0
    Views:
    292
Loading...

Share This Page