http://www.perlmonks.org?node_id=980712


in reply to Re^2: Perl list items
in thread Perl list items

"Part of my point in writing all these different versions of A is to demonstrate that choosing the same name for a scalar and an array is confusing. It is much more readable to have distinct, concise, yet descriptive names."
Is it also not a good idea to use the same name for scalar and array in a loop? I tend to do this all the time
foreach my $item (@items){ ... }
UPDATE:lol, what a blunder. I guess I use this construct so much that these names look the same to me (aside from having a different sigil), whereas they really are different.

Replies are listed 'Best First'.
Re^4: Perl list items
by frozenwithjoy (Priest) on Jul 09, 2012 at 16:17 UTC
    Well, in your example, they have different names (one is plural, one is singular). I think at this point it comes down to personal preference. It might be easy to get the similar names confused, but you won't easily call one when you mean the other by simply making a $/@ typo.
      Thank you for your help, but how would you guys take the first five items(numbers and letters) from this Scalar string that is @A? Can i use split for that?

        @A is an array. If it has five or more items in it, and you want to get just those first five out of it, you can take a slice: my @five_only = @A[0..4];

        split is for when you want to take a single scalar (an entity starting with $, such as $A, $B, or $A[0] # an element of the array @A, or a literal), and split that single scalar value into a list of elements that can be stored in an array, or processed listwise.

        join is for taking a list (a naked list, or something stored in an @Array, for example), and converting that list into a single scalar delimited by whatever you pass as your first argument to join.

        You're really pretty close. Just spend an hour with perlintro, perldata, split, and join. No more than an hour (even if it takes longer to digest every last detail, an hour is all you need to get a comfortable overview).


        Dave

        You could, but that would be missing the point, and it appears that you're still confusing arrays and scalars. @A is not a scalar string; it is an array. Its first element, $A[0] is a string, which you created by assigning a string (the result of join) to it.

        I don't know exactly what combine() does here, but it appears to be returning an array of references to arrays. If you just want the values from the first sub-array, there's no need to join them into a string and then split them out again. Just deference the array:

        my @c = combine(5,@n); # get array of arrays my @A = @{$c[0]}; # get first sub-array

        UPDATE: Thanks to Anonymous for the precedence correction.

        Aaron B.
        Available for small or large Perl jobs; see my home node.

Re^4: Perl list items
by tobyink (Canon) on Jul 09, 2012 at 16:17 UTC

    Nitpicking perhaps, but "item" and "items" are different names. :-)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'