Perl syntax can be tricky. Does this help? Or just further confuse?

Update: I guess it confused one Monk, because I got a down vote.
I am genuinely trying to be helpful here.
When you write: $y[@x], you are accessing a single element of @y with the index of the scalar value of @x.

Note: using $a and $b or @a or @b is in general not a good idea as the $a and $b variables have special meaning to Perl and are used in sort. Better is to use: x,y,z.

However: "@a return complete set of value" this is not correct. What @a would mean is context specific.

#!/usr/bin/perl -w use strict; use Data::Dumper; my @x= qw(1 2 3 4); my @y= qw(7 8 9 10 11 12 13 14); my $num = $y[@x]; # @x evaluates here to the number "4"!!! # the number of elements in @x # As said before this is a VERY unusual # formulation!!! # It looks weird because it is weird. # As mentioned above more normal would be # $y[@x-1] or $y[$#x] # I'm not saying that $y[@x] cannot have a defined, # value but just that it is "strange" absent other # context and would be worthy of a comment in the # code. print "The number in y is: $num\n"; #the 5th thing in @y!! WHOA! #print $x[@y]; # ERROR: Use of uninitialized value in print # this is because: $x[8] is undefined! # However consider this: # here [@x] means something completely different push @y, [@x]; # this means: assign new memory, # copy @x into that new memory , # push a reference to that memory # onto @y print Dumper \@y; __END__ The number in y is: 11 ### the 5th thing in @y $VAR1 = [ '7', '8', '9', '10', '11', '12', '13', '14', [ '1', '2', '3', '4' ] ];

In reply to Re^3: new to perl, syntax question by Marshall
in thread new to perl, syntax question by tics

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.