Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

new to perl, syntax question

by tics (Initiate)
on Feb 20, 2012 at 03:29 UTC ( [id://954970]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys, I'm new to perl and has question regarding this command which I came across,

my @folder; my @name; . . . $var = $folder[@name];

I understand that $folder[0] would refer to the 1st element in @folder and so on.

But what does $folder[@name] means? Is it like extracting every elements in @folder one at a time?

Thanks and regards,

Damien

Replies are listed 'Best First'.
Re: new to perl, syntax question
by ikegami (Patriarch) on Feb 20, 2012 at 03:42 UTC

    The index expression of $array[ EXPR ] is evaluated in scalar context, and the an array in scalar context evaluates to the number of elements in the array.

    my @folder = qw( a b c d e f ); my @name = qw( x y z ); my $var = $folder[@name]; # $var = 'd', since @name == 3.

      Thanks ikegami, for the wonderful explanation ! And I really appreciate the example. It really helps a lot.

      And thanks for the other suggestions as well. I'm really glad I chanced upon this forum. I did thought of making use of print to try figuring out what the statements would do, but the Internet connection here is really awful. So I gave up downloading Strawberry Perl.

      Only could resort to trying to understand the code using Notepad++.

Re: new to perl, syntax question
by Marshall (Canon) on Feb 20, 2012 at 06:28 UTC
    ikegami's post is technically correct.

    However in the code that you presented, @name has nothing to do with @folder - this should be a "red flag"!
    Something is missing here!

    This uses the number of items in @name as an index into the @folder array.
    While this could happen, it would be unusual absent other context.
    I would be looking for some code that builds @folder based upon the elements in @name.

    Update:
    Furthermore, $var = $folder[@name]; would be unusual since Perl indicies are "zero-based".
    $var = $folder[@name-1]; would I think, be more expected - another "red-flag".
    You may sometimes see $#name instead of @name-1, but this means the same thing - the last index.

      suppose you have @a= qw(1 2 3 4);
      you are writting print "\n @a"; this will print 1 2 3 4.
      if you are trying to write like...
      $p= @a, then it will give you count of array i.e. 4
      so it does not mean that if we write @a as index inside array would give the value of 4th index of array. Basically if we write code..
      $b[@a] with return nothing because bare @a return complete set of value which is not a valid index and that is passing as index in @b so of course will return blank.
        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' ] ];
Re: new to perl, syntax question
by tangent (Parson) on Feb 20, 2012 at 04:25 UTC
    Damien have a look at Arrays: A Tutorial/Reference. The relevant section is "Get count of elements" but it is well worth reading through.
    Often you can find your answer by adding a print statement, taking ikegami's example:
    print "@name"; # prints x y z print @name; # prints 3
      print evaluates its operands in list context, so print @name; does not print 3. (print scalar(@name); and print 0+@name; do, though)
        Aagh - I did try it before I replied but like this:
        print @name . "\n";
        then I took the "\n" out to make it clearer (but didn't run it!). I take it the concatenation makes print evaluate in scalar context. So should be:
        print "@name"; # prints x y z print @name; # prints xyz print @name . "\n"; # prints 3
        (lidden: I was still fixing this when you replied)

        Update - then I found this:
        If you believe in Lists in Scalar Context, Clap your Hands
        I'll be back in a few weeks.
      Update: Again just genuinely trying to be helpful with a suggestion on how to print arbitrary results - for beginners, this is WAY COOL. Didn't mean to offend anybody.

      I think better is to use the core module:

      use Data::Dumper; .... print Dumper \@name;
      Another module that you have to install before being able to use it:
      use Data::Dump qw(pp); ..... print pp \@name;
      Basically, both of these modules can print/dump an arbitrary Perl data structure.
      The format of the output is a bit different, but they both can do it.
      This is actually pretty much magic.
      It is of huge help when debugging.
      And it is of very huge help once you get into multidimensional data structures.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-26 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found