Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: can't use string as an array ref....

by inblosam (Monk)
on Jul 13, 2005 at 02:11 UTC ( [id://474437]=note: print w/replies, xml ) Need Help??


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

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
  • Comment on Re^2: can't use string as an array ref....

Replies are listed 'Best First'.
Re^3: can't use string as an array ref....
by monarch (Priest) on Jul 13, 2005 at 03:52 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://474437]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found