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

I seek the wisdom of my fellow monks. Please ponder a web survey that asks the user to rate a list of presentations from 1 to 5:

My Presentation 1() 2() 3() 4() 5()
Next Presentation 1() 2() 3() 4() 5()
There is a total of 23 presentations.

There are three peices of information I am trying to gather into a data structure:

  1. The variable name for each presentation passed to the cgi (name=s1. from s1 to s23).
  2. The proper name of each presentation.
  3. The grade of each presentations. This is the value of the variables in 1.

At first I thought about this:

my %presentations = ( s1 => "My Presentation', s2 => "Next Presentation', ... s23 => 'Last Presentation', ); # Get param answers for presentations foreach $key (keys %presentations){ $pres_answ { $presentations{$key} } = param($key); }

At that point I realized that I would have a problem with order. I want to organize the answers in the same order that they appear on the form. Hashes do not retain that order. How would you go about organizing this data in order to preserve their order?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Help creating a data structure
by Zaxo (Archbishop) on Jun 28, 2005 at 18:59 UTC

    Your key names have the form "s1" through "s23", which suggests you want an array. That will preserve order, but you could just as well sort those keys or construct an ordered list of them and take a slice.

    my %pres_answ = map { "s$_", { name => $presentations{"s$_"}, grade => param("s$_"); } } 1..23; { local ($\, $,) = ("\n", "\t"); print @pres_answ{"s$_"}{qw/name grade/} for 1 .. 23; }

    After Compline,
    Zaxo

Re: Help creating a data structure
by kirbyk (Friar) on Jun 28, 2005 at 18:45 UTC
    You can just stick a 'sort' in front of your keys and it will do what you want (with that naming convention). Well, you'll want to numeric sort (so you don't get 1, 10, 11 ..., 2, 20 kinds of sorting.)
    foreach $key (sort {$a <=> $b} keys %presentations) {
    Perl makes this job nice and easy!

    -- Kirby, WhitePages.com

Re: Help creating a data structure
by xorl (Deacon) on Jun 28, 2005 at 18:39 UTC
    Try something like:
    foreach my $key (sort keys %presentations) { $pres_answ {$presentations{$key}} = param($key); }
Re: Help creating a data structure
by fmerges (Chaplain) on Jun 28, 2005 at 21:17 UTC
Re: Help creating a data structure
by anonymized user 468275 (Curate) on Jun 29, 2005 at 09:22 UTC
    If I understand this correctly, you don't need to do anything special to preserve the order, because the order can be reconstructed using a custom sort routine.

    For example:

    my %pres = (); $pres{ s1 }{ Name } = 'name of first presentation'; $pres{ s1 }{ Grade } = 5; #etc. for my $id ( sort ById keys %pres ) { # whatever } sub ById { GetNumeric( $a ) <=> GetNumeric( $b ); } sub GetNumeric{ my $subject = shift; /(\d+)/; return $1; }

    One world, one people