foreach my $key ( keys %ARGS ) { if ( $key =~ m/^edit_name_(\d+)$/ ) { my $num = $1; # captured from the regexp my $name = $ARGS{$key}; $m->out( "<p>Edit name $num = $name</p>" ); } elsif ( $key =~ m/^edit_type_(\d+)$/ ) { my $num = $1; # captured from the regexp my $type = $ARGS{$key}; $m->out( "<p>Edit type $num = $type</p>" ); } }
The second method is to generate hashes to play with:
my %edit_name = (); my %edit_type = (); foreach my $key ( keys %ARGS ) { if ( $key =~ m/^edit_name_(\d+)$/ ) { $edit_name{$1} = $ARGS{$key}; } elsif ( $key =~ m/^edit_type_(\d+)$/ ) { $edit_type{$1} = $ARGS{$key}; } } # play with values foreach my $num ( sort { $a <=> $b } keys %edit_name ) { $m->out( "<br />Name number $num is " . $edit_name{$num} ); } foreach my $num ( sort { $a <=> $b } keys %edit_type ) { $m->out( "<br />Type number $num is " . $edit_type{$num} ); }
Of course the name and type might be related! In which case you might consider the following:
my %edit = (); foreach my $key ( keys %ARGS ) { if ( $key =~ m/^edit_([^_]+)_(\d+)$/ ) { $edit{$2}->{$1} = $ARGS{$key}; } } # now %edit contains a hash of rows. In each row # is the type and name fields. e.g. # %edit = ( 1 => { name => 'John', type => 'student' }, # 2 => { name => 'Lisa', type => 'teacher' } # ); $m->out( "<table>" ); foreach my $row ( sort { $a <=> $b } keys %edit ) { if ( ! ( exists( $edit{$row}->{name} && exists( $edit{$row}->{type} ) ) { die( "Missing field for row $row" ); } $m->out( "<tr><td>" . $edit{$row}->{name} . "</td>" . "<td> . $edit{$row}->{type} . "</td></tr>" ); } $m->out( "</table>" );
updated: cleaned up code
In reply to Re^3: can't use string as an array ref....
by monarch
in thread can't use string as an array ref....
by inblosam
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |