:
This message was edited by LUbud at 2004-9-3 10:21:2
:
This message was edited by LUbud at 2004-9-3 9:52:43
: : : I'm writing a script that contains an array of hashes.
: : :
: : : I'm using
use strict;
: : : $array[$i] = %hash;
to save the hash into the array
: : That should read:-
: :
: : $array[$i] = \%hash;
: :
: : The \ operator, when followed by a sigil, creates a reference. You cannot store a hash in a scalar, but you can store a reference to a hash. [1]
: :
: : : and afterwards I'm trying to print out the array of hashes using a for loop:
: : :
: : : for ($i = 0; $i < $5; $i++){
: : : %hash = $array[$i];
: : : print $hash{"Entry1"};
: : : print $hash{"Entry2"};
: : : }
: : :
: : :
: : : My problem is that I'm not sure how to go about storing and recalling
: : : the nested hash in the array?
: : :
: : That's OK, apart from:-
: :
: : %hash = %{$array[$i]};
: :
: : E.G. to dereference the hash reference you stored earlier. Alternatively...
: :
: : $hash = $array[$i];
: : print $hash->{"Entry1"};
: :
: : Or even just:-
: :
: : print $array[$i]->{"Entry1"};
: :
: : The ->, often named the infix operator, could be described as the "dereference operator". If you know C, think about it as being the same as a pointer deref, but with references instead of pointers.
: :
: : There's more than one way to do it.
: :
: : : Any help would be appreciated.
: : :
: : References can take a little getting used to, but IMHO are well worth taking the time to grasp. Especially as they are somewhat tied into the Perl approach to object oriented programming. OOP can be useful for some problems.
: :
: : Jonathan
: :
: : [1] In Perl 6, evaluating a hash (or array) in scalar context will result in a reference. But not in Perl 5.
: :
: : ###
: : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
: :
: :
:
: Thanks, Jonathan. I didn't know about the any of operators for referencing. I've programmed in Java so I've done some OOP. I find that Perl is somewhat confusing when it comes to syntax, other special symbols and specially referencing.
:
: I'm still having problems recalling the values from the hashes from the array. Am I recalling/referencing my hashes incorrectly when I'm printing out my data? When using
$hash = $array[$i];
: print $hash->{"Entry1"};
: or even
: print $array[$i]->{"Entry1"};
:
: to display the data, the associated values don't show. Here's a clip of my script of where I store and recall my hashes.
:
:
: for $i (0 .. 10) {
: %hash = {};
:
: foreach $entry (@entries) {
: $hash{$entry}++;
: }
: #store hash in array to be recalled later
: $array[$i] = \%hash;
: }
:
: ...
:
: #display the final data after a few minor operations
: for $i (0 .. 10) {
:
: %hash = %{$array[$i]};
:
: foreach $entry (keys %hash) {
: print "$hash{$entry}\t$entry\n";
: }
: }
:
: Any other suggestions or advice would be appreciated.
:
I have the data displaying properly except for one entry that displays is HASH(0x15d5240). I'm guessing that's a memory address but I don't know how I'm getting it or why I'm getting it.
for $i (0 .. 10) {
%hash = {};
foreach $entry (@entries) {
$hash{$entry}++;
}
#store hash in array to be recalled later
push @array, {%hash};
}
#display the final data after a few minor operations
for $i (0 .. 10) {
%hash = %{$array[$i]};
foreach $entry (keys %hash) {
print "$hash{$entry}\t$entry\n";
}
}
Any help would be helpful and appreciated.