Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Aliasing values, not variables

by Corion (Pope)
on Jul 18, 2004 at 13:01 UTC ( #375374=note: print w/ replies, xml ) Need Help??


in reply to Aliasing values, not variables

Sorry for not coming up with this ugly solution while you were asking about it in the CB:

#!/usr/bin/perl -w use strict; sub alias{ \@_ }; my @a = (1,"world"); my $arr = alias( $a[0], $a[0], $a[1] ); $arr->[0] = "hello"; print "@$arr\n";

I learned this ugly trick from demerphq while he was testing the aliasing features of his DDS module, so maybe looking at what the dumpers and their tests do might give you some more inspiration.

Updated: Fixed typo in sub alias, spotted by nothingmuch

Updated: I use strict; but the code won't run like that. That should teach me about posting when I don't cut'n'paste my code ... Spotted by The Mad Hatter


Comment on Re: Aliasing values, not variables
Download Code
Re^2: Aliasing values, not variables
by japhy (Canon) on Jul 18, 2004 at 16:23 UTC
    I don't see how this helps the situation at all. He needs to alias one element of an array to another element of the array.

    Update: hrm, I didn't follow the code properly. Yes, this is a solution to problem. My apologies, Corion.

    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      It may well be that my solution misses the goal. As far as I understood it, nothingmuch wants two slots in an array to contain the same scalar, and I believe that my subroutine alias does that.

      The routine returns a reference to an array in which the elements are aliased as passed in. So to create an array which aliases the slots 2,3 and 4, you would call it as:

      my $v; my $ar = alias("foo","bar",$v,$v,$v,"baz");

      Assigning to any of the slots 2,3,4 of @$ar will change the other two slots as well, which is how I interpret the question.

      I might well have misunderstood the question and the problem, as I also don't understand what he needs it for, or rather, what he needs it for prompts different solutions to me.

        You're right. I didn't pay attention to what your function was doing. Your solution can be expanded to work with existing aggregates in a general way.
        _____________________________________________________
        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-26 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls