#!/usr/bin/perl -w use strict; my $NB_ROWS= 5; my @FIELDS= qw( name email); sub process_params { my @data; # stores all the data retrieved as an array of hashes foreach my $nb (1..$NB_ROWS) { my $row; # stores the data for a single row: field_name => value # load the data foreach my $type ( @FIELDS) { $row->{$type}= param( "recip$type$nb"); } # error check, here just check that all fields are filled or empty row_ok( $nb, $row) or return; # this is the important part: store row only if values are filled push @data, $row if( nb_filled_fields( $row)); } return @data; } sub row_ok { my( $nb, $row)= @_; my $nb_fields= nb_filled_fields( $row); if( !$nb_fields or $nb_fields == @FIELDS) { return 1; } else { warn "bad data in row $nb"; return 0; } } # count the number of fields that have true values # you would need to use defined $_ in the grep # if 0 is a valid value for one of the parameters sub nb_filled_fields { my( $row)= @_; my @filled_fields= grep { $_ } values %$row; my $nb_filled_fields= @filled_fields; return $nb_filled_fields; } # main test and smoke and mirror subs my $test_nb=1; while( read_params()) { print "test $test_nb:\n"; my @data=process_params(); display_data( @data); $test_nb++; } sub display_data { my @data= @_; foreach my $i (0..$#data) { print "data[$i]: "; foreach my $field (@FIELDS) { print " $field => $data[$i]->{$field}"; } print "\n"; } print "\n"; } { my $params; # just a hack to simulate CGI's param method sub read_params { $params={}; # reset params local $/="\n\n"; my $data=; return undef unless $data; $data=~ s{[ \t]*\#.*\n}{}g; # remove comments my @rows= split /\n/, $data; foreach my $row (@rows) { my( $nb, $name, $email)= split /\s*:\s*/, $row; $params->{$nb}= { name => $name, email => $email }; } return 1; } sub param { my( $param_name)= @_; if( $param_name=~ m{^recip(name|email)(\d+)$}) { my( $type, $nb)= ($1, $2); return $params->{$nb}->{$type}; } else { die "wrong param called: $param_name"; } } } __DATA__ # test 1 1 : Andrew : andrew@andrew.com 2 : Joseph : joseph@joseph.com 3 4 5 # test 2 - line 4 should appear as line 2 1 : Andrew :andrew@andrew.com 2 3 4 : Joseph : joseph@joseph.com 5 # test 3 - line 4 should appear as line 1 1 2 3 4 : Joseph : joseph@joseph.com 5 # test 4 - should output warning 1 2 3 : : dummy@dummy.com 4 : Joseph : 5