jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

Pater Nosters,
I am using a hash to recreate an HTML table with radios expressing previous choices for image alignment in this intuitive order: Left, Center, Right. Problem is that, of course, the hash keys are not ordered this way and can't be sorted in any elegant way I can think of. Of course brute force can recreate the table as seen on previous pages with the appropriate radio selected but this means many more lines of code than I think should be needed. The current code is below. Any advice is, as always, greatly appreciated.
TIA
jg
my %top_align = ( Left => "left", Center => "center", Right => "right", ); while (my ($k, $v) = each %top_align) { if ($image_align_top eq $v) { print qq|<td> <div align="center"> $k <input type="radio" name="image_align_top" +value="$v" checked> </div> </td>|; } else { print qq|<td> <div align="center"> $k <input type="radio" name="image_align_top" +value="$v"> </div> </td>|; } }
_____________________________________________________
It's not my tree.

Replies are listed 'Best First'.
Re: Any Elegant Way to iterate through this hash?
by cLive ;-) (Prior) on Feb 18, 2002 at 00:05 UTC
    Without rewriting code to use CGI!!!, create an array of keys in the order you want them to be processed, and use that:
    my @sorted_keys = qw(Left Center Right); for (@sorted_keys) { my $k = $_; my $v = $top_align{$k}; if ($image_align_top eq $v) { print qq|<td> <div align="center"> $k <input type="radio" name="image_align_top" +value="$v" checked> </div> </td>|; } else { print qq|<td> <div align="center"> $k <input type="radio" name="image_align_top" +value="$v"> </div> </td>|; } }

    And forget the loop altogether...

    cLive ;-)

    ps - looking at it again, why not do something like this instead:

    my $v = lc($image_align_top); if ($v =~ /^left|center|right$/) { ...

    --
    seek(JOB,$$LA,0);

Re: Any Elegant Way to iterate through this hash?
by ehdonhon (Curate) on Feb 18, 2002 at 00:15 UTC
    my %top_align = qw( Left left Center center Right right ); foreach my $k ( keys( %top_align ) ) { my $checked = ($image_align_top eq $top_align{$k}) ? ' checked' : '' +; print qq|<td> <div align="center"> $k <input type="radio" name="image_align_top" value="$to +p_align{$k}"$checked> </div> </td>|; }

    The other method would be to use an HTML generating module like CGI

Re: Any Elegant Way to iterate through this hash?
by jerrygarciuh (Curate) on Feb 18, 2002 at 00:10 UTC
    Well, I am a little slow to think of this but using an array instead of a hash will solve the problem. Code is below.
    TIA
    jg
    my ($label, $value); my @top_align = qw(Left Center Right); while (@top_align) { $label = shift(@top_align); $value = $label; $value =~ tr/A-Z/a-z/; if ($value eq $image_align_top) { print qq|<td> <div align="center"> $label <input type="radio" name="image_align_t +op" value="$value" checked> </div> </td>|; } else { print qq|<td> <div align="center"> $label <input type="radio" name="image_align_t +op" value="$value"> </div> </td>|; } }
    _____________________________________________________
    It's not my tree.