Jim and Rob,

        Thanks to both of you for your responses and for the code you supplied. 
 That was a big help.  I was able to get it to work and I've pasted my code 
below.

> No. It is just a one-level hash.

        I'm glad you both pointed this out.  It appears that I was over 
thinking it.  Once I was able to see that, a lot of other things fell into 
place.

>> my $n = keys($main::global->{cart});
> 
> I think this can't be what you wrote - it wouldn't compile. You probably want
> 
>  my $n = keys %{$global->{cart}};

        Surprisingly, it did compile, but I like your way better.

>> my @cart_lineitems = @{$main::global->{cart}}{0 .. $n};
> 
> That is one way of doing things, but I am surprised you happened upon it 
> without knowing more about Perl.

        "Happened upon it" is exactly how I found it. =;)  Google to the 
rescue!  http://stackoverflow.com/questions/2907270/perl-convert-hash-to-array  
 And it worked - that's why I used it, but I didn't remember that hash keys 
were strings and not numbers.

> Because the values of the array elements are simple strings, as I explained 
> above, you must find a way of splitting them into individual fields.

        I took Rob's advice, since the data was tab separated, merged it with 
Jim's code, and came up with this.  Separating the sort into a sub helps, since 
I need to use it in multiple places.


foreach my $cartitem ( sort sortby_itemid keys %{$main::global->{cart}} ) {
        # list the items...
}


sub sortby_itemid {
        (split /\t+/, $main::global->{cart}->{$a})[2] cmp (split /\t+/, 
$main::global->{cart}->{$b})[2]
}

        Thanks again for your help, guys.  I really appreciate it.

Marc
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to