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

Re: Re: Perl Optimization

by Cybercosis (Monk)
on Jun 01, 2001 at 20:29 UTC ( #84982=note: print w/replies, xml ) Need Help??


in reply to Re: Perl Optimization
in thread Perl Optimization

foreach my $obj (%{$href})
# This dereference ^ causes a deep copy to be made

actually, this is more accurate:
foreach my $obj (%{$href}) #here ^ is the copy
If you check the ref()-iness of $obj, you'll find that there isn't any at all. The appropriate value is copied into $obj on each run of the loop. I'm not at all sure what it is you're trying to solve with this, but if you need access to the key/value pair without the big, horrendous copy, maybe something like this:
foreach my $key (keys %{$href}) { #do what you need with $key and $href->{$key} }
Hope you find this useful!

~Cybercosis

nemo accipere quod non merere

Replies are listed 'Best First'.
(tye)Re: Perl Optimization
by tye (Sage) on Jun 01, 2001 at 21:29 UTC

    for and foreach make the argument an alias to (not a copy of) the values in the list.

    Even if you copy a reference, you still get a reference, so I don't understand what you are trying to say.

    my $href = { 'x' => { 'y' => { 'z' => [ $xref, $yref, $zref ], }, #'strange' => { ..... }, }, #'charm' => { .... }, }; for my $obj ( %$href ) { print "$obj: ",ref($obj),$/; my $copy= $obj; print "$copy: ",ref($copy),$/; } __END__ prints: x: x: HASH(0x1a65030): HASH HASH(0x1a65030): HASH

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://84982]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2022-11-30 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?