Golf...

Discussion in 'Perl Misc' started by Peter Wyzl, Dec 5, 2004.

  1. Peter Wyzl

    Peter Wyzl Guest

    I have binary data representing an IP address (among other things) in $data.
    (part of a libpcap file, read in binary mode)

    The 4 bytes of $data repreenting the IP address start at offset 26. I am
    looking for a good solution to represent this in dotted quad. Since there
    are many repetitions of this manipulation, efficiency is important.

    What I have so far is:

    (assume $sourceip has been pre-declared and $data contains binary data)

    $sourceip = join '.', map {ord}(split '', substr $data,26,4,);

    Shorter solutions are also of interest to me.

    BTW, this is on a windows platform so the data is in little endian order in
    the headers, and network order in the raw IP packet (no reason to make life
    simple...) This IP is therefore stored in network order. I am
    contemplating a solution using unpack, but am still learing all the
    different templates, and am unsure whether it is more efficient than substr
    (or enough so to be worth trying). I have not yet benchmarked a regex
    solution, having a hunch that it would be less efficient.

    --
    Wyzelli
     
    Peter Wyzl, Dec 5, 2004
    #1
    1. Advertising

  2. Peter Wyzl

    Uri Guttman Guest

    >>>>> "PW" == Peter Wyzl <> writes:

    PW> (assume $sourceip has been pre-declared and $data contains binary data)

    PW> $sourceip = join '.', map {ord}(split '', substr $data,26,4,);

    PW> Shorter solutions are also of interest to me.

    unpack is your friend. no need for both split and substr. sprintf can do
    the ord and join in one.

    <untested>

    my $ip = sprintf '%c.%c.%c.%c', unpack 'X26CCCC', $data ;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 5, 2004
    #2
    1. Advertising

  3. Peter Wyzl

    Peter Wyzl Guest

    "Uri Guttman" <> wrote in message
    news:...
    : >>>>> "PW" == Peter Wyzl <> writes:
    :
    : PW> (assume $sourceip has been pre-declared and $data contains binary
    data)
    :
    : PW> $sourceip = join '.', map {ord}(split '', substr $data,26,4,);
    :
    : PW> Shorter solutions are also of interest to me.
    :
    : unpack is your friend. no need for both split and substr. sprintf can do
    : the ord and join in one.
    :
    : <untested>

    Thats OK, I'm happy to do the testing and playing around...

    : my $ip = sprintf '%c.%c.%c.%c', unpack 'X26CCCC', $data ;

    $sourceip = sprintf '%s.%s.%s.%s', unpack'x26CCCC', $data;

    works (the unpack C makes it a char already and %c messes with that....

    which means I can dispense with sprintf and use join again to get

    $sourceip = join '.', unpack 'x26CCCC', $data;

    12 strokes less!

    Also you meant x not X in the template for unpack (forward null bytes not
    backwards....)

    But now that does what I need, in a nice elegant way. I will attempt a
    benchmark shortly, but I suspect anything with less function calls must be
    better. 2 v 5 is a good saving.

    Incidentally, I have been able to process 5Mb (sysreading 1 packet at a
    time) in less than 3 seconds before this improvement, so this looks hopeful
    for reasonable performance on larger files..

    Thanks Uri. That was just the mental poke I needed to move forward with
    this little project.

    :)

    --
    Wyzelli
     
    Peter Wyzl, Dec 5, 2004
    #3
  4. Peter Wyzl

    Uri Guttman Guest

    >>>>> "PW" == Peter Wyzl <> writes:

    PW> "Uri Guttman" <> wrote in message
    PW> :
    PW> : <untested>

    PW> Thats OK, I'm happy to do the testing and playing around...

    want to do more testing and fixing of my code? :)

    PW> : my $ip = sprintf '%c.%c.%c.%c', unpack 'X26CCCC', $data ;

    PW> $sourceip = sprintf '%s.%s.%s.%s', unpack'x26CCCC', $data;

    PW> works (the unpack C makes it a char already and %c messes with that....

    d'oh! like i said, untested. and i was loopy then (all day in fact. :( ).

    PW> which means I can dispense with sprintf and use join again to get

    PW> $sourceip = join '.', unpack 'x26CCCC', $data;

    PW> 12 strokes less!

    and fewer calls which is more important.

    PW> Also you meant x not X in the template for unpack (forward null bytes not
    PW> backwards....)

    well i always have case issues :)

    PW> But now that does what I need, in a nice elegant way. I will
    PW> attempt a benchmark shortly, but I suspect anything with less
    PW> function calls must be better. 2 v 5 is a good saving.

    that is the general rule of thumb in perl but there are many
    counterintuitive examples especially with regexes.

    PW> Incidentally, I have been able to process 5Mb (sysreading 1 packet
    PW> at a time) in less than 3 seconds before this improvement, so this
    PW> looks hopeful for reasonable performance on larger files..

    it should be much faster IMO. post the results.

    PW> Thanks Uri. That was just the mental poke I needed to move
    PW> forward with this little project.

    i will sharpen my poker for the next time! :)

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 5, 2004
    #4
  5. Peter Wyzl

    Peter Wyzl Guest

    "Peter Wyzl" <> wrote in message
    news:gixsd.59428$...
    : "Uri Guttman" <> wrote in message
    : news:...
    :: >>>>> "PW" == Peter Wyzl <> writes:
    ::

    <snip>:

    : $sourceip = join '.', unpack 'x26CCCC', $data;

    $sourceip = join '.', unpack 'x26C4', $data;

    Is even better again...

    (guess who is finally starting to understand this function? :) And that's
    even after reading MJD's excellent tutorial at I know not where (can't seem
    to find it again)

    --
    Wyzelli
     
    Peter Wyzl, Dec 5, 2004
    #5
  6. Peter Wyzl

    Uri Guttman Guest

    >>>>> "PW" == Peter Wyzl <> writes:

    PW> "Peter Wyzl" <> wrote in message
    PW> news:gixsd.59428$...
    PW> : "Uri Guttman" <> wrote in message
    PW> : news:...
    PW> :: >>>>> "PW" == Peter Wyzl <> writes:
    PW> ::

    PW> <snip>:

    PW> : $sourceip = join '.', unpack 'x26CCCC', $data;

    PW> $sourceip = join '.', unpack 'x26C4', $data;

    PW> Is even better again...

    i should have done that too. pack/unpack are very amazing but they scare
    too many. coming from an assembler/c background, they make perfect sense
    to me.

    PW> (guess who is finally starting to understand this function? :) And
    PW> that's even after reading MJD's excellent tutorial at I know not
    PW> where (can't seem to find it again)

    sure to be on plover.com somewhere. google his site.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 5, 2004
    #6
  7. On Sun, 05 Dec 2004 04:45:15 GMT, "Peter Wyzl" <>
    wrote:

    >Subject: Golf...

    ^^^^
    ^^^^
    [snip]
    >looking for a good solution to represent this in dotted quad. Since there
    >are many repetitions of this manipulation, efficiency is important.

    ^^^^^^^^^^
    ^^^^^^^^^^

    Please note that in (Perl-)golfing efficiency is not an issue. The
    subject may have been more accurate...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 5, 2004
    #7
  8. On Sun, 05 Dec 2004 06:10:23 GMT, "Peter Wyzl" <>
    wrote:

    >(guess who is finally starting to understand this function? :) And that's
    >even after reading MJD's excellent tutorial at I know not where (can't seem
    >to find it again)


    I didn't know about that tutorial. Probably I'll search it @
    plover.com, however out of curiosity can you please anticipate how it
    compares in your opinion to 'perldoc perlpacktut'?


    Just curious,
    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 5, 2004
    #8
  9. Peter Wyzl

    Peter Wyzl Guest

    "Michele Dondi" <> wrote in message
    news:eek:...
    : On Sun, 05 Dec 2004 06:10:23 GMT, "Peter Wyzl" <>
    : wrote:
    :
    : >(guess who is finally starting to understand this function? :) And that's
    : >even after reading MJD's excellent tutorial at I know not where (can't
    seem
    : >to find it again)
    :
    : I didn't know about that tutorial. Probably I'll search it @
    : plover.com, however out of curiosity can you please anticipate how it
    : compares in your opinion to 'perldoc perlpacktut'?

    Having a quick look...

    seems they are one and the same... maybe I was reading too much of MJD's web
    site at the same time and confused them...

    My apologies to Simon and Wolfgang, and thanks to you for reminding where
    the tut was...

    :)

    --
    Wyzelli
     
    Peter Wyzl, Dec 5, 2004
    #9
    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. Mike Hoy

    my golf game needs gui

    Mike Hoy, Jun 9, 2005, in forum: Python
    Replies:
    2
    Views:
    273
    Mike Meyer
    Jun 9, 2005
  2. Carl Drinkwater
    Replies:
    7
    Views:
    506
    Paddy
    Sep 18, 2006
  3. Calvin Spealman
    Replies:
    3
    Views:
    334
    Neil Cerutti
    Sep 19, 2006
  4. J.L.Cooper

    Golf Competitions

    J.L.Cooper, Nov 12, 2004, in forum: C Programming
    Replies:
    9
    Views:
    389
    Merrill & Michele
    Nov 16, 2004
  5. luser- -droog

    any tricks to golf this code further?

    luser- -droog, Aug 3, 2011, in forum: C Programming
    Replies:
    7
    Views:
    958
    Moshbear dot Net
    Sep 11, 2011
Loading...

Share This Page