Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Array of Hashs

LUbudLUbud Posts: 27Member
[b][red]This message was edited by LUbud at 2004-9-2 11:42:6[/red][/b][hr]
I'm writing a script that contains an array of hashes.

I'm using [code]use strict;
$array[$i] = %hash;[/code] to save the hash into the array and afterwards I'm trying to print out the array of hashes using a for loop:
[code]
for ($i = 0; $i < $5; $i++){
%hash = $array[$i];
print $hash{"Entry1"};
print $hash{"Entry2"};
}
[/code]

My problem is that I'm not sure how to go about storing and recalling the nested hash in the array?

Any help would be appreciated.
Thanks


Comments

  • JonathanJonathan Posts: 2,914Member
    : I'm writing a script that contains an array of hashes.
    :
    : I'm using [code]use strict;
    : $array[$i] = %hash;[/code] 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:
    : [code]
    : for ($i = 0; $i < $5; $i++){
    : %hash = $array[$i];
    : print $hash{"Entry1"};
    : print $hash{"Entry2"};
    : }
    : [/code]
    :
    : 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.");

  • LUbudLUbud Posts: 27Member
    [b][red]This message was edited by LUbud at 2004-9-3 10:21:2[/red][/b][hr]
    [b][red]This message was edited by LUbud at 2004-9-3 9:52:43[/red][/b][hr]
    : : I'm writing a script that contains an array of hashes.
    : :
    : : I'm using [code]use strict;
    : : $array[$i] = %hash;[/code] 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:
    : : [code]
    : : for ($i = 0; $i < $5; $i++){
    : : %hash = $array[$i];
    : : print $hash{"Entry1"};
    : : print $hash{"Entry2"};
    : : }
    : : [/code]
    : :
    : : 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 [code] $hash = $array[$i];
    print $hash->{"Entry1"};
    or even
    print $array[$i]->{"Entry1"};
    [/code]
    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.

    [code]
    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} $entry
    ";
    }
    }[/code]

    Any other suggestions or advice would be appreciated.




  • LUbudLUbud Posts: 27Member
    : [b][red]This message was edited by LUbud at 2004-9-3 10:21:2[/red][/b][hr]
    : [b][red]This message was edited by LUbud at 2004-9-3 9:52:43[/red][/b][hr]
    : : : I'm writing a script that contains an array of hashes.
    : : :
    : : : I'm using [code]use strict;
    : : : $array[$i] = %hash;[/code] 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:
    : : : [code]
    : : : for ($i = 0; $i < $5; $i++){
    : : : %hash = $array[$i];
    : : : print $hash{"Entry1"};
    : : : print $hash{"Entry2"};
    : : : }
    : : : [/code]
    : : :
    : : : 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 [code] $hash = $array[$i];
    : print $hash->{"Entry1"};
    : or even
    : print $array[$i]->{"Entry1"};
    : [/code]
    : 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.
    :
    : [code]
    : 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} $entry
    ";
    : }
    : }[/code]
    :
    : 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.

    [code]
    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} $entry
    ";
    }
    }[/code]

    Any help would be helpful and appreciated.
  • WeirdofreakWeirdofreak Posts: 439Member
    : 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.
    :
    : [code]
    : 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} $entry
    ";
    : }
    : }[/code]
    :
    : Any help would be helpful and appreciated.

    The code looks fine to me, although I haven't tested it. The HASH thing is what you get when you forget to dereference, so the only thing I can think of - although there's a good chance I've missed something blindingly obvious - is that at some point during the minor operations something goes wrong. Try printing out the hash just after storing all the information. There may also be something wrong with @entries - putting hash reference into that could conceiveably mess it up I suppose, although using a reference as a key isn't something I like to think too hard about.
  • LUbudLUbud Posts: 27Member
    : : 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.
    : :
    : : [code]
    : : 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} $entry
    ";
    : : }
    : : }[/code]
    : :
    : : Any help would be helpful and appreciated.
    :
    : The code looks fine to me, although I haven't tested it. The HASH thing is what you get when you forget to dereference, so the only thing I can think of - although there's a good chance I've missed something blindingly obvious - is that at some point during the minor operations something goes wrong. Try printing out the hash just after storing all the information. There may also be something wrong with @entries - putting hash reference into that could conceiveably mess it up I suppose, although using a reference as a key isn't something I like to think too hard about.
    :

    I found the mistake I had to use () instead of {} for %hash = {};

    Thanks for your help. :)
Sign In or Register to comment.