On Aug 9, 2004, at 4:06 PM, Bob Showalter wrote:


Here's the approach I would take (not tested!)

  my %data;
  for (param()) {   # loop through all parameters
      if (/^([qtdp])(\d+)$/) {
          my $prefix = $1;
          my $num = $2;
          $data{$num}{prefix} = param($_);
      }
   }

Basically, this inverts the data so you have a hash like:

  (
     '1' => { q => 1, t => 1, d => 'something 1', p => 3.45 },
     '2' => { q => '', t => 2, d => 'something 2', p => 1.90 },
     ... and so on
  }

Now, let's find the groups that have something in q, d, or p:

my @groups = grep "@data{$_}{qw/q d p/}" =~ /\S/, sort { $a <=> $b } keys
%data;


Now you can write those groups out, renumbering as you go:

  my $n = 0;
  for my $g (@groups) {
      $n++;
      print "$_$n $g->{$_}\n" for qw/q t d p/;
  }




Thanks Bob,

That's certainly much tighter code than I've come up with. I'm probably making a beginners mistake, but I couldn't get it to output the result I'm after. I do see the logic though and I may play with it some more.

Bill



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to