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

hello

i am try to put values in an array with STDIN after finishing putting the values i am trying to print the array and not success

this is my program:

#! /usr/bin/perl -w use strict; print "Enter 5 numbers: \n"; my $i=0; my @arr1=(); while (($i<=4) and (<>)) { @arr1 = (@arr1,$_); $i++; } my $m; foreach $m (@arr1) { print "$m \n"; }
and this is the error message that perl write me:
use of uninitialized value in concatenation or string at ...path...lin +e 17,<> line 5.

Code tags added by Arunbear

2006-08-26 Retitled by g0n, as per Monastery guidelines
Original title: 'STDIN'

Replies are listed 'Best First'.
Re: Input to array from STDIN, then print, but print is not working
by Tanktalus (Canon) on Aug 26, 2006 at 15:18 UTC

    First off, please try to learn how to use the preview button. If things don't look easy to read to you, probably it isn't easy for us to read, either.

    Second, the diamond operator, <> is only special in special circumstances. You need to do something like this:

    while (($i <= 4) and ($_ = <>))
    This is so that when you add $_ to @arr1, it actually has something in it. Actually, we've lost a lot of specialness here already. So you also likely need to add 'defined' like this:
    while (($i <= 4) and defined ($_ = <>))
    You also should look at things like push as a way to add something to the end of an array.

Re: Input to array from STDIN, then print, but print is not working
by imp (Priest) on Aug 26, 2006 at 15:35 UTC
    The handling of $_ is somewhat magical, and provided by the perl parser - not by the angle brackets. When perl sees this:
    while (<>) { }
    It actually does this:
    while (defined($_ = <ARGV>)) { (); }
    You can verify this yourself using B::Deparse. Here is an example (assume test.pl as filename):
    perl -MO=Deparse test.pl
    Problems:
    1. By adding the test for $i <= 4 to the while loop you caused perl to skip the magical behaviour for while(<>).
    2. You should chomp the data that is being read in, otherwise instead of "1" you will have "1\n"
    Suggestions:
    1. It is a better practice to use push instead of (@arr1,$_)
    2. use warnings instead of perl -w

    New version of the code that addresses the above

    #!/usr/bin/perl use strict; use warnings; print "Enter 5 numbers: \n"; my $i=0; my @arr1=(); while (<>) { chomp; push @arr1, $_; last if ++$i > 4; } foreach my $m (@arr1) { print $m, "\n"; }

    Update

    Additionally, please read How To Ask A Question - Choose a Good, Descriptive Title
Re: Input to array from STDIN, then print, but print is not working
by davido (Cardinal) on Aug 26, 2006 at 16:06 UTC

    Try this:

    use strict; use warnings; print "Enter five numbers (hit ctrl-d to terminate final input):\n"; my @array; while( <> ){ last if $. > 4; chomp; push @array, $_; } print "$_\n" foreach @array;

    This uses special variable $. to automatically count the input line for you. This eliminates the need for "$i <= 4". You could replace "last if $. > 4;" with "last if $i++ > 4;" to similar effect.


    Dave

Re: Input to array from STDIN, then print, but print is not working
by GrandFather (Saint) on Aug 26, 2006 at 22:22 UTC

    You have good answers that address your immediate problems. However it looks like you are starting out on your Perl journy. May I suggest that you spend a little time perusing the Tutorials section here and especially Getting Started with Perl, Getting Deeper Into Perl and Pattern Matching, Regular Expressions, and Parsing (which is one of the reasons for choosing Perl after all).

    Although I've never programmed Lisp, @arr1 = (@arr1,$_); has a Lispish flavour to it. In Perl it is more conventional to use push (as shown in some of the other answers) to add elements to the end of an array, or unshift to add elements to the start of an array. In similar fashion pop and shift are used to remove the element from the end and start of an array respectively.

    Other list processing functions that are well worth being aware of are join, map and grep. For example the print loop could be rewritten using join as:

    print join "\n", @arr1, '';

    Note, the , '' on the end ensures a newline is emitted following the last array element.


    DWIM is Perl's answer to Gödel