Your skill will accomplishwhat the force of many cannot PerlMonks

Perl list items

by robertw (Sexton)
 on Jul 09, 2012 at 15:16 UTC ( #980702=perlquestion: print w/ replies, xml ) Need Help??
robertw has asked for the wisdom of the Perl Monks concerning the following question:

Dear perl monks, I have made the following script

```use lib '/opt/perl5/perlbrew/bin/perlbrew/perls/perl-5.14.0/lib/site_p
+erl/5.14.0';
use Games::Cards::Poker;
use Math::Combinatorics;
@n = ("2","3","4","5","6","7","8","9","T","J","Q","K","A");

@A =  join(",", map { join ",", @\$_ } combine(5,@n));

\$A= @A;
print \$A;

Why does this generate 1 instead of the items in the list? If you put the list items in a scalar you should be able to get the number from the scalar this way right?

Comment on Perl list items
Replies are listed 'Best First'.
Re: Perl list items
by davido (Archbishop) on Jul 09, 2012 at 15:22 UTC

1 is the number of elements in the list.

This line:

```@A = join( "," .....

Creates a single element that is a string containing a bunch of comma delimited data.

This line:

```\$A = @A;

Evaluates @A in scalar context, which causes Perl to assign a count of the number of elements in @A to \$A.

Those issues will be discussed in perlintro, and join.

In particular, the purpose of join is to turn a list into a single scalar value.

Dave

To elaborate on davido's good answer, if you had used ( \$A ) = @A instead, @A would have been evaluated in list context and \$A would have the value of the first element of \$A, which could have also been written as \$A[0] (i.e., the 1st element of @A).

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.

One more thing... When troubleshooting your code, it can be helpful to print the contents of your variables to make sure things look as expected. This is simplest for scalars. For more complex structures, Data::Dumper and Data::Printer are very handy. I prefer Data::Printer since you need to type less, there is auto-indexing and pretty color coding. Here is a usage example (it is more impressive on complex hashes, etc.):

```use Data::Printer;
p @A;
"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.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://980702]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2015-08-28 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?