Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: hash slice ? No thanks, I'm about to return...

by leriksen (Curate)
on Feb 20, 2005 at 23:18 UTC ( [id://432912]=note: print w/replies, xml ) Need Help??


in reply to Re^3: hash slice ? No thanks, I'm about to return...
in thread hash slice ? No thanks, I'm about to return...

I had considered trying to take this off-line, but if I am still confused, maybe others are.

My question is, why does the list assigned to not include the keys as well ?

And perhaps can you give an example that doesn't use $_ with all its aliasing behaviour just adding to the noise.

...it is better to be approximately right than precisely wrong. - Warren Buffet

  • Comment on Re^4: hash slice ? No thanks, I'm about to return...

Replies are listed 'Best First'.
Re^5: hash slice ? No thanks, I'm about to return...
by Tanktalus (Canon) on Feb 21, 2005 at 02:59 UTC

    The value of a line such as:

    $x = 1;
    is 1. Obviously. But the reason it's one is not because $x is now set to 1. It's because the RHS is 1. Thus, if I do this:
    $x[0] = 1;
    This too is 1. Not @x.

    Hope this helps.

Re^5: hash slice ? No thanks, I'm about to return...
by Aristotle (Chancellor) on Feb 21, 2005 at 04:39 UTC

    What does this print?

    print join ' ', @return{ @keys };

    Why would an assignment behave differently? And if it should include the keys, then what should happen if you said this?

    ( $foo, $bar, @return{ @keys } ) = 1 .. 10;

    And perhaps can you give an example that doesn't use $_ with all its aliasing behaviour just adding to the noise.

    The aliasing behaviour isn't noise, it is exactly the point. The aliasing behaviour makes a difference because the assignment returns the list of lvalues that were assigned to. If the assignment returned copies, then nothing in the original array would change. Let's do that again, but making sure to pass copies this time:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %return; my @value = qw( foo bar baz quux ); $_ = uc $_ for @{ [ @return{ qw( a b c d ) } = @value ] }; print Dumper( \%return ), Dumper( \@value ); =begin output $VAR1 = { 'c' => 'baz', 'a' => 'foo', 'b' => 'bar', 'd' => 'quux' }; $VAR1 = [ 'foo', 'bar', 'baz', 'quux' ];

    This time, the changes are lost in the mist of time, because we didn't operate on the list of lvalues assigned to, we operated on copies of their values.

    Makeshifts last the longest.

      Firstly, thanx Tanktalus and Aristotle for your patience in trying to get me to understand.

      I think its starting to become clear. The key phrase, as from your first reply post to this, Aristole, is the list of lvalues assigned to. I dont think I really understand what that actually means - so I'll try to explain what I think it means.

      Originally I thought it mean the list of keys and values, but now I see it means just the values part, not the keys. In fact assigning to a key doesnt seem to make much sense - in a hash, a key has a related value, and you can assign to that value, but you can't assign to a key. Except maybe initially, which has the effect of creating the key. I dunno, maybe my language is too loose.

      The values being assigned to are the values of the keys, thats all.

      That said, Aristotle, I really dont understand what your trying to show me with your examples

      Why would an assignment behave differently - I understand that the values returned by the hash slice are joined, but I dont understand how the join and the assignment to a slice are related.

      The aliasing behaviour isn't noise, it is exactly the point. I completely dont understand what your trying to show me ?

      Sorry if I'm being thick, I think I'm getting closer if that helps.

      ...it is better to be approximately right than precisely wrong. - Warren Buffet

        Ah. Well, “lvalue” means “value which can appear on the left side of an assignment” which means “something that can be assigned to,” as opposed to a constant or some such. So “the list of lvalues assigned to” means “a list of aliases to the things you just changed.” And that is just what my examples show.

        You are right that one cannot assign to a key. One can create or delete keys, but not manipulate them in any way. (In fact, internally, hash keys are plain strings, not fullblown scalars. That is why you can't use references as keys and expect those keys to still work as references.)

        I used the join as an example of a hash slice, because slices work in assignment just as they work anywhere else. By using a hash slice you query the hash for a list of values, specified by the given list of keys, and what you get is that list of values. You can assign to that list just as you can assign to ( $foo, $bar, $baz ).

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-24 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found