Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] Perl Problem

Expand Messages
  • merlyn@stonehenge.com
    ... Kenneth Please do not use phonetic shorthand English that is popular during cell Kenneth phone texting if you want me to read and (hopefully) respond
    Message 1 of 11 , Jul 19, 2010
      >>>>> "Kenneth" == Kenneth Wolcott <kennethwolcott@...> writes:

      Kenneth> Please do not use phonetic shorthand English that is popular during cell
      Kenneth> phone texting if you want me to read and (hopefully) respond to your
      Kenneth> posting.

      Seconded.

      As I say on IRC,

      "if you're paying for IRC by the character, sucks to be u". :)

      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
      Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
      See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
    • andy_bach@wiwb.uscourts.gov
      Another usage note Perl Problem isn t much of a subject line - er, what *else* would you be writing to the list with? ;- perl -00lne push @{ $h{ $1 } },
      Message 2 of 11 , Jul 20, 2010
        Another usage note "Perl Problem" isn't much of a subject line - er, what
        *else* would you be writing to the list with? ;->


        perl -00lne '
        push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;
        }
        {
        print for map @{ $h{ $_ } }, sort { $b <=> $a } keys %h;' /tmp/h.dat

        -00 is used to set perl to 'slurp' in paragraph mode (see perldoc perlrun
        -0 (that's zero) option). -l is to add newlines to print stmts. -n is is
        in auto-while loop and -e is to mark the script. Those unmatched curlies
        are a trick to get an loop and END block - because -n takes your code and
        wraps it in while loop
        while (<>) {
        <-e code>
        }

        adding a close then open curly, in the middle of your code make it:
        while (<>) {
        <-e code up to close curly>
        }
        {
        <-e code after curlies>

        push @{ $h{ $1 } }, $_ if /of ([0-9]+) bytes/m;

        finds the digits before the word "bytes", uses it as a hash key for %h,
        which is then treated as an anonymous array and the whole line saved
        therein <ahem> (that sentence is as convoluted as this code - sorry ;-).
        The "m" flag isn't needed (?) as there's no "." in the RE, I believe. I
        don't recall the data set but they may have meant/needed to use "\s+" for
        the whitespace (if the data were wrapping on lines, say).

        print for map @{ $h{ $_ } }, sort { $b <=> $a } keys %h;' /tmp/h.dat

        after the data's been processed, we fetch back that # of bytes key, sort
        them reverse numerically ("$b" before "$a") and print each line of the
        anonymous array for that key.

        #!/usr/bin/perl -00l
        use strict;
        use warnings;
        my %data_lines;
        while (<>) {
        next unless /of (\d+) bytes/;
        push @{ $data_lines{$1} }, $_;
        # warn("adding $1 - $_");
        }
        foreach my $key ( sort { $b <=> $a } keys %data_lines ) {
        print "Bytes: $key";
        foreach my $line ( @{ $data_lines{$key} } ) {
        # whatever you want, line by line now
        print "\tdata: $line";
        } # foreach line
        } # foreach key


        a

        ----------------------
        Andy Bach
        Systems Mangler
        Internet: andy_bach@...
        Voice: (608) 261-5738;
        Cell: (608) 658-1890

        "Every time I see an adult on a bicycle, I no longer despair for the
        future of
        the human race."
        H. G. Wells, 1904

        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.