Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: new to perl, syntax question

by tangent (Vicar)
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

Replies are listed 'Best First'.
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 (Abbot) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954977]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-06-20 06:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.