Difference between list and arrays.

2014-09-16 Thread Uday Vernekar
Confusion on array and list. can anybody explain me the difference between
list and arrays.

my @xyz = ( 4, 5, 6 );

The right-hand side of the equals sign is a list.here I assign that list to
the variable @xyz.
its an array,list can be assigned to an array.

on similiar lines
we can assign Lists to hashes
my %zzz = ( a = 42, b = 43, b = 44 );




-- 
*
Don't ask them WHY they hurt you,
because all they'll tell you is lies and excuses.
 Just know they were wrong, and try to move on.
**


Re: Difference between list and arrays.

2014-09-16 Thread Andy Bach
On Tue, Sep 16, 2014 at 4:22 AM, Uday Vernekar vernekaru...@gmail.com
wrote:

 Confusion on array and list. can anybody explain me the difference between
 list and arrays.

 my @xyz = ( 4, 5, 6 );

 The right-hand side of the equals sign is a list.here I assign that list
 to the variable @xyz.
 its an array,list can be assigned to an array.

 on similiar lines
 we can assign Lists to hashes
 my %zzz = ( a = 42, b = 43, b = 44 );



Not sure exactly what your confusion is, you are correct. Couple notes, one
is, the hash assigned list uses the super comma (=) which is actually
just a fancy comma; it quotes the left hand side for you, so:
( a = 42, b = 43, b = 44 );

is the same as (note, 2nd and 3rd keys were b which'd overwrite the 43
w/ 44 so I made it a c:
( a, 42, b, 43, c, 44 );

That is, the hash assignment just requires (well, Perl will assign undef
if you're short a value and complain if you've warnings set[1]) an even
number of elements; key, value, pairs.  You can do:
my @uvw = ( a = 42, b = 43, b = 44 );

and end up w/ 6 elements (in order) in the array.  The other thing to think
about is context - the LHS of the assigning = determines how the RHS
list is treated. In scalar context
my $one_var = (1, 2, 3);

the list returns its element count (as does an array - for hashes, it's
similar but you get a fraction: # of elements/buckets [2]) so
$one_var == 3;

In list context, you get assignment, as you see above or:
my ($one_var) = (1,2,3);

Here, the first element on the RHS gets assigned to the first member of the
LHS, i.e.
$one_var == 1;

The rest get thrown away.  If the LHS has an array, it gets everything
else, if there's too many LHS scalars, they get assigned undef.
-- 

a

[1]
# perl -we 'my %h = (b = 3, a); print $h{a}\n;'
Unquoted string a may clash with future reserved word at -e line 1.
Odd number of elements in hash assignment at -e line 1.
Use of uninitialized value in concatenation (.) or string at -e line 1.

[2]
http://stackoverflow.com/questions/7427381/what-do-you-get-if-you-evaluate-a-hash-in-scalar-context


Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: Difference between list and arrays.

2014-09-16 Thread Paul Johnson
On Tue, Sep 16, 2014 at 12:12:05PM -0500, Andy Bach wrote:

The other thing to think
 about is context - the LHS of the assigning = determines how the RHS
 list is treated.

So far, so good.  Well, almost ...

  In scalar context
 my $one_var = (1, 2, 3);
 
 the list returns its element count

But, I'm afraid, this is a common misconception.  The fact is that there
is no such thing as a list in scalar context.

So what, then, is the construct above?

You were correct in saying that it (whatever it is) is evaluated in
scalar context.  In scalar context, the parentheses () are used simply
for precedence.  The commas are operators.

The comma operator evaluates its LHS, throws it away, evaluates its RHS
and returns that.  The comma operator is left associative (see perlop).

So the result of evaluating the RHS (1, 2, 3) is:

  (1, 2, 3) - ((1, 2), 3) - (2, 3) - 3

And this is why $one_var gets the value 3 - not because there are three
elements in a list on the RHS.

This all becomes easier to understand if you don't use the values 1, 2
and 3 :)

See also perldoc -q 'difference between a list and an array'

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Difference between list and arrays.

2014-09-16 Thread Andy Bach
On Tue, Sep 16, 2014 at 1:45 PM, Paul Johnson p...@pjcj.net wrote:

 The comma operator evaluates its LHS, throws it away, evaluates its RHS
 and returns that.  The comma operator is left associative (see perlop).

 So the result of evaluating the RHS (1, 2, 3) is:

   (1, 2, 3) - ((1, 2), 3) - (2, 3) - 3

 And this is why $one_var gets the value 3 - not because there are three
 elements in a list on the RHS.


D'oh! Somewhere I knew something like that, that a list in scalar context
ends up assigning the last element to the scalar - though I'm pretty sure I
didn't know it was due to the comma operator.  I should have though, as:
# perl -we 'my $one = (3,2,1); print $one\n'
Useless use of a constant in void context at -e line 1.
Useless use of a constant in void context at -e line 1.
1

Those two warnings are the dropping of the result of the first to comma
operations.  Hmm, so this works [1]:

my $second_elem = ('first', 'second', 'third')[1];
$second_elem eq 'second';

because ...???

 This all becomes easier to understand if you don't use the values 1, 2
and 3 :)

I knew that was a bad idea ... just not enough to not use it.
$ perldoc -q 'difference between a list and an array'
...
As a side note, there’s no such thing as a list in *scalar context*.  When
you say

   $scalar = (2, 5, 7, 9);

   you’re using the comma operator in *scalar context*, so it uses the
scalar comma operator.  There never was a list there at all!  This causes
the last value to be returned: 9.

Well, I would've found that if perldoc would've found this for queries on
scalar context, list context, array context or, for that matter,
plain old context but ..


-- 

a

[1]
perl -we 'my $s_e = (x, y, z)[1]; print $s_e\n'

same as:
perl -we 'my $s_e = qw(x y z)[1]; print $s_e\n'

and:
perl -we 'my ($s_e) = qw(x y z)[1]; print $s_e\n'



Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: Difference between list and arrays.

2014-09-16 Thread Paul Johnson
On Tue, Sep 16, 2014 at 02:43:28PM -0500, Andy Bach wrote:
 On Tue, Sep 16, 2014 at 1:45 PM, Paul Johnson p...@pjcj.net wrote:
 
  The comma operator evaluates its LHS, throws it away, evaluates its RHS
  and returns that.  The comma operator is left associative (see perlop).
 
  So the result of evaluating the RHS (1, 2, 3) is:
 
(1, 2, 3) - ((1, 2), 3) - (2, 3) - 3
 
  And this is why $one_var gets the value 3 - not because there are three
  elements in a list on the RHS.
 
 
 D'oh! Somewhere I knew something like that, that a list in scalar context
 ends up assigning the last element to the scalar - though I'm pretty sure I
 didn't know it was due to the comma operator.  I should have though, as:
 # perl -we 'my $one = (3,2,1); print $one\n'
 Useless use of a constant in void context at -e line 1.
 Useless use of a constant in void context at -e line 1.
 1
 
 Those two warnings are the dropping of the result of the first to comma
 operations.  Hmm, so this works [1]:
 
 my $second_elem = ('first', 'second', 'third')[1];
 $second_elem eq 'second';
 
 because ...???

Aha, turning it up a notch ;-)

In this case, ('first', 'second', 'third') is actually a list.  Why?
Because the [1] indexes into it.  The [1] forces it into list context.
You can't index into something that is in scalar context.

 $ perldoc -q 'difference between a list and an array'

 Well, I would've found that if perldoc would've found this for queries on
 scalar context, list context, array context or, for that matter,
 plain old context but ..

Yes, I think the search is only on the title of the section, not its
contents.  Which can mean that you need to find what you are looking for
in order to know how to find it.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/