Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: new to perl, syntax question

by tangent (Deacon)
on Feb 20, 2012 at 04:25 UTC ( #954977=note: print w/ replies, xml ) Need Help??


in reply to new to perl, syntax question

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


Comment on Re: new to perl, syntax question
Download Code
Re^2: new to perl, syntax question
by ikegami (Pope) on Feb 20, 2012 at 05:05 UTC
    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.
        Concatenation puts its arguments in scalar context, that is the array is in scalar context.
Re^2: new to perl, syntax question
by Marshall (Prior) on Feb 20, 2012 at 10:53 UTC
    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
Node Status?
node history
Node Type: note [id://954977]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-10-24 21:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (137 votes), past polls