Bill Stephenson wrote:
> Hi all,
> 
> I need some help. I have name/value parameters coming in from a web
> form that look something like this (blank lines added for clarity):
> 
>       firstname=bill
>       lastname=stephenson
> 
>       q1=1
>       t1=y
>       d1=something 1
>       p1=3.45
> 
>       q2=
>       t2=y
>       d2=something 2
>       p2=1.90
> 
>       q3=
>       t3=y
>       d3=
>       p3=
> 
>       q4=1
>       t4=y
>       d4=something 3
>       p4=12
> 
>       q5=
>       t5=y
>       d5=
>       p5=
> 
>       q6=3
>       t6=y
>       d6=something 4
>       p6=1.22
> 
>       id_number=10000259
>       action=none
> 
> I want to remove the groups of data that look like "q3-p3" and "q5-p5"
> (where "q","d", and "p" are all empty, "t" will always have data) then
> re-number the groups that contain data in in at least one of the
> "q","d", or "p" variables, then save them to a file that looks
> something like this:
> 
>       firstname=bill
>       lastname=stephenson
> 
>       q1=1
>       t1=y
>       d1=something 1
>       p1=3.45
> 
>       q2=
>       t2=y
>       d2=something 2
>       p2=1.90
> 
>       q3=1
>       t3=y
>       d3=something 3
>       p3=12
> 
>       q4=3
>       t4=y
>       d4=something 4
>       p4=1.22
> 
>       id_number=10000259
>       action=none
> 
> I'm having trouble with the logic that will accomplish this. It
> doesn't seem like it should be very hard to do, but I just can't seem
> to say it in "perl".

[snip code]

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/;
  }

-- 
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