Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
The stupid question is the question not asked
 
PerlMonks  

Hashes & Arrays

by Bugorr (Novice)
on Aug 29, 2005 at 19:42 UTC ( #487521=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, but did not solve my problem!!!
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!!!

code tags by holli

Comment on Hashes & Arrays
Select or Download Code
Re: Hashes & Arrays
by GrandFather (Cardinal) on Aug 29, 2005 at 19:50 UTC

    Start with perllol and perldata then do a Super Search for HoAoH.

    When you have sample code that you are having trouble with, post a follow up question here and we will leap in to help.


    Perl is Huffman encoded by design.
      Dear vroom unfortunately I have no code and need help with that :)

        A first step is to read the documentation. Then draw the data as it will be structured on paper, or other suitable material of your choice, using indentation to show the nesting of the various elements and either {}, () or [] as appropriate to show the type of element. So for example and array of arrays (AoA) might look like this:

        ( ["1 in 1", "2 in 1", "3 in 1"], ["1 in 2", "2 in 2", "3 in 2", "4 in 2"], ["1 in 3", "2 in 3"] )
        Then you can do stuff like
        my @array = ( ["1 in 1", "2 in 1", "3 in 1"], ["1 in 2", "2 in 2", "3 in 2", "4 in 2"], ["1 in 3", "2 in 3"] );

        to create the array of arrays. Of course until you show us a little code so that we can see what you are trying to achieve it is hard to help much more than that. And yes, I do know that you are after a different data structure than that, but I'm sure with a little work you can at least get to the point of writing some code, even if it doesn't do what you expect.


        Perl is Huffman encoded by design.
      Hey GrandFather, we shuold have a "Homework" node, no?

      Celebrate Intellectual Diversity

Re: Hashes & Arrays
by InfiniteSilence (Deacon) on Aug 29, 2005 at 19:52 UTC
    Well, if this is homework you still need to read up on several things:
  • Perl Arrays
  • Perl Hashes
  • Autovivification (sp?)
    perl -MData::Dumper -e "%Rhash = ('email'=>'email\@email',timestamp=>s +calar localtime()); $dummyArray[0] = \%Rhash; $Phash{'woowo'} = \@dum +myArray; print Dumper %Phash;" $VAR1 = 'woowo'; $VAR2 = [ { 'email' => 'email\\@email', 'timestamp' => 'Mon Aug 29 14:50:52 2005' } ]; C:\>

    Celebrate Intellectual Diversity

      Most people forget to escape special characters. You did the opposite and added a slash to the email address. When using single quotes or the q quote-like operator, \ is not an escape character unless followed by the string literal terminator (single quote) or another \. That's not the case here. 'email\@address' should be 'email@address' or "email\@address".
Re: Hashes & Arrays
by Joost (Canon) on Aug 29, 2005 at 19:56 UTC
    The perldata and perlreftut manpages should explain the basics. If you have a standard perl install, you can read those by typing man perldata or perldoc perldata - see the perl manpage for an index of all the standard documentation.

    Anyway, I'd do it something like this:

    my %hash = ( # declare top-level hash key => [] # create new empty arrayref with key "key" ); # create a new anonymous hashref with the data and # push it onto the array push @{$hash{key}}, { email => 'email@address', timestamp => time }; # that will allow you to get at the first email address # with the following code: my $email = $hash{key}[0]{email};

    If you want to access by your given example ($hash{key}{array}[0]{email}) you need an extra hash:

    my %hash = ( key => { array => [] } ); push @{$hash{key}{array}}, { email => 'email@address', timestamp => time };
    updated: fixed bug that ikegami found.
      Bug: "email@address" should be 'email@address' or "email\@address", otherwise @address will get interpolated. Under strict vars, this won't even compile.
Re: Hashes & Arrays
by ikegami (Pope) on Aug 29, 2005 at 19:58 UTC

    I think you mean $email = $hash{key}[0]{email}; At least, that's what your steps describe.

    my %hash; # Inputed data: my $email = 'email@example.com'; my $timestamp = time; # Adding a record: my $record = { email => $email, timtstamp => timestamp, }; push(@{$hash{key}}, $record); # Example of getting a value: print("email for first record: ", $hash{key}[0]{email}, "\n");

    "@{ $hash{key} }" means "the array at key 'key' in the hash '%hash'". Auto-vivification will create the array for us if it doesn't already exist.

Re: Hashes & Arrays
by zby (Vicar) on Aug 29, 2005 at 19:59 UTC
    I don't really understand what is your problem - but reading perldoc perldsc should help you.

    And perhaps what can you help as well is playing with the code you posted:

    $hash{key}[0]{email} = 'email@email'; $hash{key}[0]{timestamp} = time;
    This sets the first element of the array to the hash  {email => 'email@email', timestamp => time} (the {array} in your example seems to be redundant). And to set the second element of the array:
    $hash{key}[1]{email} = 'email@email'; $hash{key}[1]{timestamp} = time;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://487521]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2014-04-17 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (449 votes), past polls