Printing number of elements in an array

Discussion in 'Perl Misc' started by Derek Basch, Apr 17, 2006.

  1. Derek Basch

    Derek Basch Guest

    I just started with Perl after programming in Python for many years and
    I must say it is driving me friggin nuts. For instance why does the
    first method of printing the number of elements in an array work but
    the second prints nothing?

    print $temp = @fields;

    $temp = @fields;
    print $temp

    Thanks,
    Derek Basch
    Derek Basch, Apr 17, 2006
    #1
    1. Advertising

  2. "Derek Basch" <> writes:
    > I just started with Perl after programming in Python for many years and
    > I must say it is driving me friggin nuts. For instance why does the
    > first method of printing the number of elements in an array work but
    > the second prints nothing?


    Assuming you put

    my @fields = qw/one two three/;

    > print $temp = @fields;
    >
    > $temp = @fields;
    > print $temp


    This works fine. Both print statements output '3'. Of course, you
    didn't put a newline between them, so you'll see '33' on your screen,
    but that's a technicality of output.

    -=Eric
    Eric Schwartz, Apr 17, 2006
    #2
    1. Advertising

  3. Derek Basch

    Derek Basch Guest

    Ahhh. Found it. I had not put a line terminator after:

    print $temp

    It should have been:

    print $temp;

    Now I get an integer either way.

    Thanks,
    Derek
    Derek Basch, Apr 17, 2006
    #3
  4. Derek Basch

    John Bokma Guest

    Marc Dashevsky <> wrote:

    > use warnings;
    > use strict;
    >
    > my @fields = (0..8);
    > my $temp;
    >
    > print $temp = @fields, "\n";
    > $temp = @fields;
    > print $temp, "\n";


    how about print scalar( @fields), "\n"; ?

    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
    John Bokma, Apr 17, 2006
    #4
  5. Derek Basch

    Derek Basch Guest

    Thanks for the replies everyone!

    Derek Basch
    Derek Basch, Apr 17, 2006
    #5
  6. Derek Basch wrote:
    > Ahhh. Found it. I had not put a line terminator after:
    >
    > print $temp
    >
    > It should have been:
    >
    > print $temp;
    >
    > Now I get an integer either way.


    There is no need to put a semicolon after a Perl statement. It's not a
    terminator, it's a separator. Was this perhaps not the last line in
    your code?

    Note: you should get into the habit of putting "use strict'" and "use
    warnings" at the top of even the simplest script. Doing so would
    probably have caused Perl to higlight your error. In this case it
    would probably have got upset that you were trying to use the string
    "3" in a context where a file handle was exepected and said...

    Can't use string ("3") as a symbol ref while "strict refs" in use

    Without "use strict" Perl will silently convert the string "3" into a
    reference to a filehandle called 3 and you'd get a warning...

    print() on unopened filehandle 3

    Without "use warnings" either Perl will silently discard output sent to
    an unopened file handle.
    Brian McCauley, Apr 18, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. P
    Replies:
    1
    Views:
    1,160
    Joe Kesselman
    Jul 7, 2006
  2. crazy

    printing array elements

    crazy, Oct 1, 2003, in forum: C Programming
    Replies:
    7
    Views:
    497
    Dan Pop
    Oct 2, 2003
  3. Herrcho
    Replies:
    6
    Views:
    447
    Peter Pichler
    Feb 20, 2004
  4. Replies:
    4
    Views:
    944
    Martin Ambuhl
    May 20, 2005
  5. Hicham Mouline
    Replies:
    1
    Views:
    390
    Kai-Uwe Bux
    Apr 11, 2010
Loading...

Share This Page