Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-06-2009, 05:00 AM   PM User | #1
bazz
Senior Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 4,330
Thanks: 35
Thanked 151 Times in 147 Posts
bazz will become famous soon enoughbazz will become famous soon enough
error when looping through hash.

Hi,

Getting the hang of hashes now (yay!) but have got stuck.

I have a working solution at the end of this post but is it the best way?

my hash, retrieved from session, is like shown below and my two foreach loops throw an error. (also shown).

How can I output the room numbers in order, without getting this error?

Quote:
Can't use string ("1") as a HASH ref while "strict refs" in use at .... line 223
Code:
 'product_id_room' => {
                                                       '46' => '1',
                                                       '48' => '3',
                                                       '47' => '2'
                                                     },
here is my loop

Code:
foreach my $product_id (sort keys %product_id_room_numbers) {

    foreach my $room_number (sort keys % {$product_id_room_numbers{$product_id}}) {
 
print qq( product_id=$product_id :: room_number = $room_number <br />);

  }
}
Then I read about perl not liking single digit numbers in these circumstances so I found this bit of code. Is it correct? It seems to work but I would like the best way to do it.

Code:
foreach my $product_id (sort {$product_id_room_numbers{$a} cmp $product_id_room_numbers{$b} }
           keys %product_id_room_numbers) {
    my $room_number = $product_id_room_numbers{$product_id};
  
print qq(
product_id = $product_id : room_number = $room_number );
bazz
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Old 11-06-2009, 05:39 PM   PM User | #2
oesxyl
Senior Coder


 
Join Date: Dec 2007
Posts: 4,658
Thanks: 374
Thanked 562 Times in 551 Posts
oesxyl has a spectacular aura aboutoesxyl has a spectacular aura about
use <=> operator instead of cmp when you compare numbers
Code:
sort { $product_id_room_numbers{$a} <=> $product_id_room_numbers{$b} } keys %product_id_room_numbers;
best regards
oesxyl is offline   Reply With Quote
Old 11-06-2009, 07:40 PM   PM User | #3
bazz
Senior Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 4,330
Thanks: 35
Thanked 151 Times in 147 Posts
bazz will become famous soon enoughbazz will become famous soon enough
Thank you oesxyl,

I wondered about that but I thought the website I visited (tutorial), was more knowledgable than me so I used their suggestion. Time to trust my own sense.

bazz
__________________
"The day you stop learning is the day you become obsolete"! - my late Dad.
Useful MySQL resource
Useful MySQL link
bazz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:11 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.