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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't coerce array into hash - yep i'm at it again :-) 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hello Guys,

Ok i'm trying (unsuccesfully as you can tell by the error message) to push 1 element of an array of hashes into a seperate array.

here's the code..
Code:
push my @lterm, @rs->{'Term'};

is there an easy way of doing this - converting an array of hashes to an array of single scalars choosing which element of the hash to use?

thanks 1DMF
 
Are you saying that you want to put the value(s) of a hash into an array? If so, your syntax is wrong.

Code:
push my @lterm, $rs->{Term};

Or, you can do this:

Code:
push my @lterm, $rs1;
push my @lterm, $rs2;
# where both $rs1 and $rs2 are references of hashes.

 
No I'm saying I have an ARRAY OF HASHES and want to create an array of just 1 key value of the array of hashes.

just what my foreach loop does but with a one line command
 
If I understand correctly, you want @lterm to contain the value of `Term' that's in each hash that you have in your @rs array(?)

If that's right, this should do it:
Code:
my @lterm = map $_->{Term}, @rs;
 
thanks ishnid, I thought there must be a way!

does it work faster and more efficient than the foreach loop - it does seem too!

and why on earth did you think you didn't understand what I was after, do i really speak that much double dutch?

hhmm maybe you shouldn't answer that :)
 
oopps missed the code

Code:
# Create additional array to hold terms
foreach my $dt (@rs) {
    push @lterm, $$dt{'Term'};
}

this was how I was doing it
 
According to a quick Benchmark, it seems that using `map' is faster:
Code:
             Rate foreach     map
foreach 1492537/s      --    -32%
map     2188184/s     47%      --
I assume this comes from avoiding repeated calls to `push', in favour of creating a list and assigning it all to the array in one go.

As for your ``double dutch'' - I generally just like to spell-out what my code is trying to do if I'm not absolutely certain what's being sought. It has prevented problems of solving the wrong problem by mistake in the past. You had made reference to a foreach loop that I hadn't seen, so that threw me a little.
 
I know - I re-read the thread and realised my mistake - sorry - glad you did understand what I meant!.
 
Yeah, map is designed for translating a list, so it should be quickest at the task. The foreach as you had it has a couple things that may also be slowing it down. First, Perl's recreating my $dt each time it loops, which is some overhead, along with the extra scope created by the { braces }. Just a guess, but this might benchmark a little closer (while being a bit less legible than either above method):
Code:
push @lterm, $_->{'Term'} for(@rs);
but it still has the large number of calls to push, which would have to get redimensioned much more often.

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top