xorl has asked for the wisdom of the Perl Monks concerning the following question:

I'm starting to think what I want to do can't be done. But I figured if it can be done y'all will know.
my @bar = qw(One Two Three); my $foo = "bar"; print "***" . $foo . "***\n"; foreach my $baz (@$foo) { print $_ . "\n"; }
The output I want is:
***bar***
One
Two
Three
Now I tried $foo = \@bar, but I got the expected Array reference stuff when I printed it. Is there any easy way to keep $foo as a string and still be able to use to access the array or if I make $foo the array reference is there an easy way to get the sting bar from it? I'm also using v5.6.1 built for MSWin32-x86-multi-thread if that makes any difference. Thanks

Replies are listed 'Best First'.
Re: Create an array reference from a string
by Corion (Patriarch) on Apr 12, 2004 at 19:28 UTC
      So I guess you're suggesting something like:
      my @bar = qw(One Two Three); my $foo = "bar"; my %quux = ("bar", \@bar); print "***" . $foo . "***"; foreach my $baz (@{$quux{$foo}}) { print $baz . "\n\n"; }
      It looks like that will work, but it does mean I have to create a hash.
        Think about it this way - you are asking to access some data structure (scalar, array, hash, whatever) using an arbitrary string as its name. That is the very definition of a hash.

        In fact, soft references are implemented under the hood as a hash. So, it's not like you're saving anything. :-)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Create an array reference from a string
by jdporter (Paladin) on Apr 12, 2004 at 19:32 UTC

    What you're talking about is known as "symbolic references". It won't work on lexical ("my") variables; your @bar would have to be a package (aka "global") variable.

    But while symbolic refs can be done, they are strongly discouraged. Instead, consider using a hash variable:

    my %h; @{$h{"bar"}} = qw( One Two Three ); $foo = "bar"; for ( @{ $h{$foo} } ) {

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Create an array reference from a string
by dragonchild (Archbishop) on Apr 12, 2004 at 19:26 UTC
    What is the thing you want to do? There are a number of answers, depending on what you want. Your example doesn't really make sense.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I want to print the string that $foo is equal to and then print the values in the array that has the same name as what $foo is equal to. Does that help?
Re: Create an array reference from a string
by Ovid (Cardinal) on Apr 12, 2004 at 19:47 UTC

    I think that you may have made a bad choice of string value for $foo in that some other posters think that you want to interpolate the value of $bar. Is it the case that you want two distinct and possibly unrelated values for $foo, one a string and the other an array reference? If so, I would suggest that you might have a design flaw somewhere (which a hash solution might fix), but on the off chance that you really need this, you could use overload -- though I wouldn't recommend it.

    #!/usr/local/bin/perl -w use strict; + package ArrayOrString; use overload '""' => \&to_string; + my $STRING_VAL = ''; + sub new { my ($class, $arrayref) = @_; die "not an array ref" unless 'ARRAY' eq ref $arrayref; bless $arrayref, $class; } + sub string { shift; $STRING_VAL = shift if @_; return $STRING_VAL; } + sub to_string { return $STRING_VAL; } + package main; + my @bar = qw(one two three); my $foo = ArrayOrString->new(\@bar); $foo->string('bar'); print "*** $foo ***\n"; + foreach my $baz (@$foo) { print "Val: $baz \n"; }

    Update: Whoops! I see your reply to dragonchild. You want soft references and that's a bad idea. Ignore my neat code, it doesn't do what you want, though it does what I said it does :)

    Cheers,
    Ovid

    New address of my CGI Course.

      This is Cool, Ovid. I wish I could give more than just a ++ to your answer. Thanks.
Re: Create an array reference from a string
by boby_drack (Acolyte) on Apr 12, 2004 at 20:48 UTC
    i don't know if i had understood your problem but this :
    my @bar = qw(One Two Three); my $foo = "bar"; print "***" . $foo . "***\n"; my($baz); foreach $baz (@bar) { print $baz. "\n"; }
    it will give you this output :
    ***bar***
    One
    Two
    Three

    that many ways to do it (sic !)
Re: Create an array reference from a string
by Sandy (Curate) on Apr 14, 2004 at 23:31 UTC
    I'm late in the game here... but anyways...

    Without arguing the merits of symbolic reference (merits they say... what merits?), this will do it.

    #!/usr/bin/perl our @bar = qw(One Two Three); my $foo = "bar"; print "***" . $foo . "***\n"; foreach (@$foo) { print $_ . "\n"; }