CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Manipulating a Hash with keys that have more than one value

 

First page Previous page 1 2 Next page Last page  View All


spuds
Novice

Mar 20, 2011, 3:12 PM

Post #26 of 35 (2412 views)
Re: [Karazam] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

No, other source script (mostly yours) took a string (rulenumber='4') just fine ... must be something else...


Karazam
User

Mar 20, 2011, 3:16 PM

Post #27 of 35 (2410 views)
Re: [spuds] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

New code snippets popping up. Smile
I thing you need to post the whole of your program, and attach some data file(s) too,
it would make it so much easier to find out what's wrong.


spuds
Novice

Mar 20, 2011, 3:21 PM

Post #28 of 35 (2408 views)
Re: [spuds] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

The %rules hash output from your source ... only change I made is to populate the test data there exactly like I did in my script, using:

$rules{$rulekey1} = [ $contents1, $ruleNum1 ];

Both your source and my source scripts take there input the same way (line above), show the EXACT same hash when I do a print Dumper(%rules) at the same point the scripts, but yours run without any array ref "1" error.

Thank you.

- spuds Smile


Karazam
User

Mar 20, 2011, 3:38 PM

Post #29 of 35 (2403 views)
Re: [spuds] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

How do you create "$contents1" if you get the Dumper result you posted earlier? Mine is


Code
$VAR1 = 'cRule'; 
$VAR2 = [
'rulenumber=3',
'3'
];
$VAR3 = 'dRule';
$VAR4 = [
'rulenumber=4',
'4'
];



spuds
Novice

Mar 20, 2011, 3:57 PM

Post #30 of 35 (2400 views)
Re: [Karazam] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

Hi,

Please take a look at the reply I sent "you".

$contents1 is just the contents of a file. (which could be '# rulenumer=2' or anything else)

Thank you.

- spuds


spuds
Novice

Mar 20, 2011, 7:14 PM

Post #31 of 35 (2388 views)
Re: [spuds] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

Hi,

Here is the test data and how I fill the HoA with the script that works.

# test data
my $ruleKey1 = "aRule-REQUEST";
my $contents1 = "# rulenumber=1";
my $ruleNum1 = 1;

my $ruleKey2 = "bRule-REQUEST";
my $contents2 = "# rulenumber=11";
my $ruleNum2 = 11;

my $ruleKey3 = "cRule-REQUEST";
my $contents3 = "# rulenumber=4";
my $ruleNum3 = 4;

# add keys and their values to an anonymous array
$rules{$ruleKey1} = [ $contents1, $ruleNum1 ];
$rules{$ruleKey2} = [ $contents2, $ruleNum2 ];
$rules{$ruleKey3} = [ $contents3, $ruleNum3 ];

Only difference in the one that throws the array ref "1" error is that the data there is retrieved from the local file system files, getting the $contents from one file at a time.

Thank you.

- spudsWink


spuds
Novice

Mar 20, 2011, 7:59 PM

Post #32 of 35 (2383 views)
Re: [spuds] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

Hi,

Is the syntax correct here:

map $_->[0], sort { $a->[1] <=> $b->[1] }

# next line? shouldn't there be a couple more brackets on the right side '[' and ']'?
map [ [ $_, @{ $$href{$_} }[0] ], @{ $$href{$_} }[1] ],
keys %{$href};


Thank you.

- spuds


spuds
Novice

Mar 20, 2011, 8:47 PM

Post #33 of 35 (2379 views)
Re: [spuds] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

Hi,

Why are you using a local hash called %seen ?

I am getting the same array ref not found on line 266 below:

sub get_sorted_rules
250 {
251 my $href = shift;
252 my $rType = shift;
253
254 print "inside get_sorted_rules ... ruleType is $rType ...\n\n";
255
256 my %seen;
257 my $count = 0;
258
259 print %{$href};
260
261 for my $z ( sort_rules($href) )
262 {
263 print "the number of time thru this for loop $count\n";
264 print "z is now: $z\n";
265
266 print "seen hash contains $seen{$z->[1]}\n\n";
267
268 unless( $seen{$z->[1]} )
269 {


Each print %{$href} is printing out:

cRule-REQUESTARRAY(0x9f8eeb0)aRule-REQUESTARRAY(0x9b56680)

so the %rules hash looks fine... Again, the local %seen hash logic above still working in your source but not in my complete source script. Will look into more tomorrow.

Thank you.

- spudsSmile


Karazam
User

Mar 21, 2011, 1:00 PM

Post #34 of 35 (2353 views)
Re: [spuds] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

I have looked at the code you sent me, but cannot find anything in there that explains the errors you are getting.

The syntax in the sort routine is alright.

The %seen hash is there to exclude duplicates, like


Code
dRule rulenumber=4 4 
bRule rulenumber=4 2


The second field is saved as a hash key, so in the next run of the loop we can check if it's already there.

One thing, there's this line:


Code
if ( $1 > MAX_RULES ) { ...


I suppose you have a


Code
use constant MAX_RULES => 8; # example value

somewhere in your code?


spuds
Novice

Mar 21, 2011, 3:45 PM

Post #35 of 35 (2351 views)
Re: [Karazam] Manipulating a Hash with keys that have more than one value [In reply to] Can't Post

Hi,

Got it working. Only problem I found was a subroutine call with one to many arguments.

Thank you very much for all your help!

- spuds

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives