Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Perl assign scalar to array

by bagyi (Acolyte)
on Oct 12, 2015 at 13:27 UTC ( [id://1144527]=perlquestion: print w/replies, xml ) Need Help??

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

Just a quick question
my @a; # code that init @a @a[2..5] = 2;

This assignment sets a elements at 2,3,4,5 to 2. The same works for hash slice. I would like to know if there is any documentation? And details of working. Is there any general concept behind like auto-vivification. I have looked in perl data tutorial.

Oops sorry it is not working. I didn't use strict while testing this out.

Replies are listed 'Best First'.
Re: Perl assign scalar to array
by davido (Cardinal) on Oct 12, 2015 at 14:47 UTC

    Your code fails because it assigns a single scalar value to the lvalue list produced from a hash an array slice. Only one of the elements in the slice will be populated. To populate each element, you can use the x operator in list context.

    my @a; @a[2..5] = (2)x4;

    ...or, using map

    @a[2..5] = map 2, 2..5;

    ...or, using a foreach loop...

    $a[$_] = 2 for 2..5;

    There are many other ways to do it as well. Just pick the one that is easiest to read.


    Dave

      [This post is obsolete]

      @a[2..5] is an array slice, not a hash slice. @h{'a','b'} would be a hash slice.

        Thanks for finding a typo. Fixed.


        Dave

Re: Perl assign scalar to array
by Athanasius (Archbishop) on Oct 12, 2015 at 13:38 UTC

    Hello bagyi,

    You’re right, it’s not working, but you don’t need to use strict to see that:

    23:34 >perl -MData::Dump -wE "my @a = ('a'..'j'); @a[2..5] = 2; dd \@a +;" ["a", "b", 2, undef, undef, undef, "g" .. "j"] 23:35 >

    (Assigning 2 here is equivalent to assigning a list with 2 as the only element. The missing elements default to undef.)

    Slices are documented in perldata#Slices.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Assigning 2 here is equivalent to assigning a list with 2 as the only element.

      That's not right. It's not equivalent to assigning to a list; it is assigning a list. That should read "You are assigning a list consisting of 2 as its only element." I believe you think that @a=2 and @a=(2) are different, but they aren't. Parens don't create lists.

        Hello ikegami,

        I had to think carefully about this, but of course you’re right. Without parentheses:

        13:54 >perl -MData::Dump -wE "my @c = 2, 3; dd \@c;" Useless use of a constant (3) in void context at -e line 1. [2] 16:15 >

        the assignment occurs first, because the precedence of the assignment operator is higher than that of the comma operator. So the fact that assignment to an array puts the right-hand side into list context is irrelevent here. With scalar context imposed instead:

        16:35 >perl -wE "my $c = 2, 3; say $c;" Useless use of a constant (3) in void context at -e line 1. 2 16:36 >perl -wE "my $c = (2, 3); say $c;" Useless use of a constant (2) in void context at -e line 1. 3 16:36 >

        the parentheses act only to change the order of evaluation, because parenthesized expressions (“terms”1) have the highest precedence. So in a standard array assignment:

        16:36 >perl -MData::Dump -wE "my @c = (2, 3); dd \@c;" [2, 3] 16:36 >

        the assignment to an array puts the RHS into list context; the parentheses create a term; and within that term, the comma operator is “just the list argument separator, and inserts both its arguments into the list.”2 I think that makes sense. :-)

        Thanks for the clarification,

        1perlop#Terms-and-List-Operators-(Leftward).
        2perlop#Comma-Operator.

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Perl assign scalar to array
by johngg (Canon) on Oct 12, 2015 at 14:11 UTC

    You can use the list multiplier.

    $ perl -Mstrict -Mwarnings -MData::Dumper -E ' > my @arr = q{a} .. q{j}; > my $low = 2; > my $high = 5; > @arr[ $low .. $high ] = ( 2 ) x ( $high - $low + 1 ); > print Data::Dumper->Dumpxs( [ \ @arr ], [ qw{ *arr } ] );' @arr = ( 'a', 'b', 2, 2, 2, 2, 'g', 'h', 'i', 'j' ); $

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1144527]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found