Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Updated Hashes & Arrays

by Bugorr (Novice)
on Aug 29, 2005 at 20:48 UTC ( #487554=perlquestion: print w/replies, xml ) Need Help??

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

Thank you everyone for your replys - they were very helpfull, its partially solved the mystery, but not completely!!!
Here's a little more detailed explanation:

I need to read a file (line by line of course).
Line looks like that "subject^email^timestamp"

After file is read I need to do Insertion into database. Now, subjects could be the same and I decided to create this datastructure:
%subjects{$subject}{%hash => email, timestamp} (hope it's clear so far)
%subjects is hash, which has "subject" as a key
$subject is array, which holds hashes or emails and times

So, I created this code:
my %subjects; foreach my $subm(@subm){ ($subject, $email, $time) = split(/^/, $subm); my %info; $info{email} = $email; $info{time} = $time; if(! exists $subjects{$subject}){ print "Key DOES NOT exist\n"; $subjects{$subject} = []; }else{ print "KEY DO EXIST\n"; } push @{$subjects{$subject}}, %info; }
I have this code to check that everything is correct:
foreach my $key (sort (keys %subjects)){ print "\n\nKEY = $key"; my @ar = @{subjects{$key}}; my $size = @ar; print "\nSize of Array = $size"; }
Unfortunately, Size gives me size =1 , which is not true. Please tell me if I am doing something wrong....

Thank you very much!!!

Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: Updated Hashes & Arrays
by davidrw (Prior) on Aug 29, 2005 at 21:19 UTC
    push @{$subjects{$subject}}, %info;
    The elements of the array need to be hashrefs, so do this instead:
    push @{$subjects{$subject}}, \%info; print "email: " . $subjects{$subject}->[0]->{email};
    Please use strict; and use warnings .. the line my @ar = @{subjects{$key}}; is missing a "$" .. also note you can write that output as:
    foreach my $key (sort (keys %subjects)){ print "\n\nKEY = $key"; print "\nSize of Array = " . scalar @{$subjects{$key}}; }
Re: Updated Hashes & Arrays
by shemp (Deacon) on Aug 29, 2005 at 21:12 UTC
    Your code is nearly impossible to read. Please use CODE tags in your post, and a reasonable indentation scheme.

    One apparent problem outside of that, you say $subject is an array. You probably mean arrayref, but anyway using an arrayref as a hash key works, but probably doesnt give you the results you're looking for.

    I use the most powerful debugger available: print!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://487554]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2022-12-04 21:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found