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

Re: [PBML] Perl Problem

Expand Messages
  • 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 1 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


      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.