in reply to can't use string as an array ref....

So, after looking at your code, I'm guessing what you want is to process a form that looks something like
<input type="text" name="edit_name_1" value="hello" size="20"> <input type="text" name="edit_name_2" value="world" size="20"> <input type="text" name="edit_name_3" value="form" size="20">
..where you have a number of edit_name_n fields.

I suggest you do the following:

foreach my $key ( keys %ARGS ) { # skip any arguments that aren't of interest next if ( $key !~ m/^edit_name_(\d+)$/ ); my $num = $1; # captured from the regexp my $name = $ARGS{$key}; $m->out( "<p>Edit field $num = $name</p>" ); }

Replies are listed 'Best First'.
Re^2: can't use string as an array ref....
by NetWallah (Canon) on Jul 13, 2005 at 01:14 UTC
    monarch has a nice, clean and safe proposal.

    The only thing I'd suggest is a sort befor keys, so that you get the edits in order.

         "Income tax returns are the most imaginative fiction being written today." -- Herman Wouk

Re^2: can't use string as an array ref....
by inblosam (Monk) on Jul 13, 2005 at 02:11 UTC
    That works really great, thank you! I have more than one field (I simplified it for the sake of easy duplication), so besides edit_name_N I may have edit_type_N, what is the best way to work that in too? Another "next if" won't work, and when I just do an if it gives me everything posted.

    Thanks a ton! I like this way better than the other way from the other node I think.


    Michael Jensen
      There are numerous ways of approaching this. Two suggestions are:
      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