Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Assigning value to the array elements

by anniyan (Monk)
on Oct 28, 2005 at 13:07 UTC ( #503635=perlquestion: print w/ replies, xml ) Need Help??
anniyan has asked for the wisdom of the Perl Monks concerning the following question:

I have a question in assigning value to the array elements which inturn a scalar variable.

my $first = ''; my $second = ''; my $third = ''; my $fourth = ''; my $ +fifth = ''; my $sixth = ''; my @vars = ('$first', '$second', '$third', '$fourth', '$fifth', '$sixt +h'); my @aarr = ('<chap', '<book', '<isb'); for (@aarr) { $vars[$i] = $_; $i++; } print "$first\t$second\t$third\n";

Whether it is possible or not?

updated

Expected output: <chap <book <isb

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Comment on Assigning value to the array elements
Select or Download Code
Re: Assigning value to the array elements
by wfsp (Abbot) on Oct 28, 2005 at 13:49 UTC
    Hi anniyan!

    Could you show us what you expected the output to look like?

Re: Assigning value to the array elements
by wfsp (Abbot) on Oct 28, 2005 at 14:13 UTC
    Perhaps use a hash to hold the output?
    #!/bin/perl5 use strict; use warnings; my @vars = qw( first second third fourth fifth sixth ); my @aarr = ('<chap', '<book', '<isb'); my %out; # hash to hold output my $i = 0; for (@aarr) { $out{$vars[$i]} = $_; $i++; } print "$out{first}\t$out{second}\t$out{third}\n"; __DATA__ ---------- Capture Output ---------- > "c:\perl\bin\perl.exe" _new.pl <chap <book <isb > Terminated with exit code 0.
Re: Assigning value to the array elements
by Fletch (Chancellor) on Oct 28, 2005 at 14:15 UTC

    Erm, it appears that you want to tie scalars to individual elements of an array if I'm reading your question correctly. Why exactly you'd want to do this and what it'd gain you is escaping me at the moment but the easiest way to get the behavior would be to make $first and friends references to elements of @vars.

    my @vars; my $second = \$vars[1]; my $i = 0; for ( qw( able baker charlie ) ) { $vars[ $i++ ] = $_; } print "second: ", $$second, "\n";

    But again, what that'd be useful for isn't really clear. Read perlreftut and perlref, and perhaps explain what you think doing this buys you. You could also do something with a tied scalar, but that's slightly more complicated.

Re: Assigning value to the array elements
by ioannis (Priest) on Oct 28, 2005 at 14:31 UTC
    You can assign into elements of an array if they are variables. All work is in one line -- within the print statement.
    use List::MoreUtils qw(pairwise); my ($first, $second, $third, $fourth, $fifth, $sixth); my @vars = \($first, $second, $third, $fourth, $fifth, $sixth); my @aarr = ('<chap', '<book', '<isb'); print {$,=' '; \*STDOUT} pairwise { ${$a} = $b} @vars, @aarr;

      Thanks that works perfectly for my use. Also thanks for Fletch, wfsp and no_slogan.

      The reason i am using this is, i dont know exactly how many values will be in @aarr. But i want to assign those values to some scalar variables which i will use in my program later for another use. If there is any alternative please suggest

      Regards,
      Anniyan
      (CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

        But i want to assign those values to some scalar variables which i will use in my program later for another use. If there is any alternative please suggest
        Change the rest of the program so it doesn't expect the values to appear in scalar variables. Really. Your code will be much easier to understand if it says this:
        my $first = $config_values{first}; open my $file, $first;
        rather than just blithely saying the second line and leaving the poor reader to figure out how $first got its value.
Re: Assigning value to the array elements
by no_slogan (Deacon) on Oct 28, 2005 at 14:50 UTC
    Several people have offered alternatives to what you're trying to do, and that's good. But nobody has told you why you shouldn't be doing it, and that's explained by MJD.

    Because I am a bad person, I'll tell you that you can do what you want by a) saying ${$vars[$i]}=$_, b) removing the "my" from $first='' and so on, and c) removing the dollar signs from @vars. But don't do that.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-07-25 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (172 votes), past polls