Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Hash of a nArray and all that comes with it.

by kingram (Acolyte)
on Jul 28, 2012 at 05:32 UTC ( #984147=perlquestion: print w/ replies, xml ) Need Help??
kingram has asked for the wisdom of the Perl Monks concerning the following question:

Srsly. I'm not a n00b.
But This has thing has me scrambled.
An hour in and I'm flustered and no google searching or "Perl By Example" reading is helping.
I know it's something really freaking stoopid but c'mon. I think maybe I'm just tired.
Ok anyway here it is:
%entry = {$item_id=>\@data, $item_id=>\@data.....etc}
%entry is a hash populated by a foreach loop that runs through a set of about 200 items, breaking them apart for atomicity of the data (is that a real word I used just there?)
for my $key (sort keys %entry) { #print Dumper($entry{$key}[1]); my @entry; for (my $i = 0; $i <= $#entry; $i++){ print $key, " ========================= \n"; for (my $j =0; $j <= 10; $j++){ print "Entry[$i][$j] = $entry[$i][$j] \n"; } } }

As long as I give an explicit value for $j's range, the output is fine. But I cannot figure out how to access the arraysize for $entry[$i]

And if there is a simpler way to do this that I could implement with my limited expertise in perl I'd love to hear some ideas.

Thanks!

Comment on Hash of a nArray and all that comes with it.
Select or Download Code
Re: Hash of a nArray and all that comes with it.
by moritz (Cardinal) on Jul 28, 2012 at 05:43 UTC
    %entry = {$item_id=>\@data, $item_id=>\@data.....etc}

    I hope that's not what you're doing, because that assigns a hash reference to a hash without dereferencing.

    As long as I give an explicit value for $j's range, the output is fine. But I cannot figure out how to access the arraysize for $entry[$i]
    my $len = scalar @{ $entry{$key}[$i] }

    gives you the length of the $i array stored at $key.

    And I think you're missing the part of your code where you assign to @entry, I can't see how you'll get any output like this. @entry = @{ $entry{$key} } should fix it.

      This is how %entry is being populated in the foreach loop:
      @update = ($id, $title, $foo, $bar, $baz); $entry{$file_num} = \@update;
Re: Hash of a nArray and all that comes with it.
by 2teez (Priest) on Jul 28, 2012 at 05:55 UTC

    this:

    %entry = {$item_id=>\@data, $item_id=>\@data.....etc};
    should be written as either:
    $entry = {$item_id=>\@data, $item_id=>\@data,.....etc}; ## or %entry = ($item_id=>\@data, $item_id=>\@data,.....etc);

      Isn't ##or what I wrote? Or did you mean that to be something else?
        Nevermind. Was hard to see the braces vs parens.
Re: Hash of a nArray and all that comes with it.
by aitap (Deacon) on Jul 28, 2012 at 06:47 UTC

    First of all, as it was pointed above, you should either create a reference to a hash (my $reference = { ... }) or just a hash (my %hash = ( ... )), note the brackets.

    Next, contents of hash should be accessed using curly, not square brackets (the second is also possible because hashes can be treated as arrays). Example:

    my %entry = ($item_id=>\@data, $item_id=>\@data); print $entry{$i}->[$j]; # contents of the hash ({}) are array referenc +es; we use -> to work with references; then we use [] to work with ar +rays # OR: my $reference = {$item_id=>\@data, $item_id=>\@data}; print $entry->{$i}->[$j]; # firstly, $entry is a reference to a hash, use -> # secondly, hash contents are accessed using {} # thirdly, it's again a reference # fourthly, arrays are accessed using []

    Next, there is a special syntax to treat a variable as a reference: you can always use an array reference, in curly braces, in place of the name of an array. To get the index of an array by its reference, write $# and then write the reference in curly brackets:

    my $index = $#{$entry{$i}};

    See perlreftut for more.

    Sorry if my advice was wrong.
Re: Hash of a nArray and all that comes with it.
by kingram (Acolyte) on Jul 29, 2012 at 01:55 UTC
    Ok. You wanna hear something REALLY stoopid?
    No. Really.
    In my life as a programmer, the wrench that I consistently thrown in my own work is phenomenal.
    Ok. Here it is:
    I've been trying to get a scalar from $#{entry{$key}}
    The problem is, that's not the same as $#{$entry{$key}}
    I am such a Rookie!
    Thanks for the 2nd, 3rd, 4th and 5th pair of eyes folks.
    Happy programming!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-09-21 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls