in reply to Reading and producing variable lists (w/o hashes!)

I think it would be easier for us to understand what exactly you're trying to do, if you posted the complete snippet of code you've tried, together with some sample input and a description of what you'd expect to happen...

  • Comment on Re: Reading and producing variable lists (w/o hashes!)

Replies are listed 'Best First'.
Re^2: Reading and producing variable lists (w/o hashes!)
by bingohighway (Acolyte) on May 01, 2009 at 10:17 UTC
    ok, massively simplified example:
    my $one = ("$apple", "$pear"); my $two = "$orange"; $$one->[0]= "hello"; $$two = "goodbye"; print "$apple $orange";
    Neither of these work. I know it is something simple I am not doing.

    Cheers!

      Maybe something like this?

      my @one = ('$apple', '$pear'); my $two = '$orange'; eval "$one[0] = 'hello'"; # evaluates $apple = 'hello' eval "$two = 'goodbye'"; # evaluates $orange = 'goodbye' print "$apple $orange"; # hello goodbye

      (Note the single quotes in '$apple' etc. With double quotes, the (empty) global variable $apple would be interpolated.)

      Update: Another way would be to use symbolic references:

      use strict; use warnings; my @one = qw($apple $pear); my $two = '$orange'; our ($apple, $pear, $orange); { no strict 'refs'; ${substr $one[0],1} = 'hello'; # ${'apple'} = 'hello'; ${substr $two,1} = 'goodbye'; # ${'orange'} = 'goodbye'; } print "$apple $orange"; # hello goodbye

      (You'd need package variables and no strict 'refs' in this case, though)

        oo crazy cool.. Two questions though: what might be a 'good' place to use this? and does it run under 'strict'?
        ........
        Those are my principles. If you don't like them I have others.
        -- Groucho Marx
        .......