Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is my hash an array ? 1

Status
Not open for further replies.

1DMF

Programmer
Joined
Jan 18, 2005
Messages
8,795
Location
GB
I just can't work this out, i've created a scalar which is an array reference and when I make an array = the array ref and then loop the array $_ = an array , yet it is a hash.

can anyone straighten me out on that is going on.

module has
Code:
# Return Record Set Array of Hashes
	$self->{'RS'} = \@rs;

scripts has...
Code:
my @recs = $sql->{'RS'};
for(@recs){
    print $_ . "<br />";
}

and what gets printed is
ARRAY(0x171b4d0)

All I want to do is create an object that has a variable which is an array of hashes, how difficult can this be, or how dumb must i be, because i cant work this out.

I've tried..
Code:
@{ $self->{'RS'} } = @rs;

so I could then do
Code:
for($sql->{'RS'}){
    print $_ . "<br />";
}

but that just prints an array reference as well.

I know 100% that @rs is an array of hashes, but i can't seem to turn it into an object with a variable that is an array of the hashes and then use it in my script.

hope this makes sense to someone!


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I've given up, and decided to use the method in a different manner

I've put it back to returning an array of hash refs and then used it like this
Code:
my $sql = new Sql_mod;
$sql->set ('Cols','FullName');
[b]my @rs = $sql->get;[/b]
for(@rs){
    print $_->{'FullName'} . "<br />";
}

it seems far to much trouble trying to create a variable within the object which is the array of hash refs, all I keep getting back is array refs and hash refs withing array refs and hash refs and can never get to the data, I make this simple change and it all works fine.

why is it so hard to create a variable with in the object to behave just like @varname , why is it converting it into some kind of weird internal looping making it an array of an array of an array and if you dereference the array $_ is a hash ref and if you put %in front it's still a hash ref and if you use the hash ref key $_->{'keyname'} that's a hash ref, bizzare if you ask me!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Code:
 $self->{'RS'} = \@rs;
Here, $self->{'RS'} is a reference to an array. If I understand you correctly, it's that @rs array that stores your hashes.

Code:
my @recs = $sql->{'RS'};
for(@recs){
    print $_ . "<br />";
}
Assign $sql->{'RS'} to an array. However, $sql->{'RS'} isn't actually an array, it's a reference to an array. Therefore @recs is now an array with one element: the reference to the array that you've stored in $sql->{'RS'}. This is why it prints ARRAY(0x171b4d0). You'll need to dereference $sql->{'RS'} if you're to be able to loop through the hashes that are stored in your array, like so:
Code:
my @recs = @{ $sql->{'RS'} };
for(@recs){
    print $_ . "<br />";
}
 
hey ishnid, I did try that , but had the same thing, and then I kept getting the hash is hash is a hash problem.

and don't forget I also did
Code:
@{ $self->{'RS'} } = @rs;

which should if i'm right in thinking make $self->{'RS'} an array (copy of @rs).

so I should in theory then be able to to do
Code:
for($self->{'RS'}){print $_->{'HashKey'}

but again that didn't work, having to use it in the manner you show
my @recs = @{ $sql->{'RS'} };
would also make it ugly to use in the script anyhow.

so i thing my @rs = $sql->get; , is nicer anyhow , don't you?

have a good weekend :-)



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Use Data::Dumper to check your data structure:

Code:
use Data::Dumper;
print Dumper \@recs;


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top