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

Hi Monks , I am trying to come up with a script that would read an emplyee info and save it into a file, SO I am promting the user for name , locaton, and age. then they should be stored in a file as follow: personName Location Age I am trying :
!#/usr/local/bin/perl my @EMP_INFO; print "What is your name \n"; my $input_name = <STDIN>; push @EMP_INFO, <STDIN>; print "What is the location name\n"; my $input_location = <STDIN>; push @EMP_INFO, <STDIN>; print "What is the age \n"; my $input_age = <STDIN>; push @EMP_INFO, <STDIN>; for ( my $x=0; $x < @EMP_INFO; $x++ ) { print @EMP_INFO; }

Replies are listed 'Best First'.
Re: reading input
by duff (Parson) on Feb 20, 2004 at 21:15 UTC

    What you want is

    chomp(my $input_name = <STDIN>); push @EMP_INFO, $input_name;

    Or perhaps you might want to abstract things ever so slightly:

    sub Query { print "@_\n"; chomp(my $ans = <STDIN>); return $ans; } push @EMP_INFO, Query("What is your name?"); push @EMP_INFO, Query("What is the location?"); push @EMP_INFO, Query("What is the age?");

    And your loop is very unperlish. A more perlish way to write it would be:

    for my $ei (@EMP_INFO) { print "$ei\n"; }

    Or maybe even

    print map { "$_\n" } @EMP_INFO;

    That's assuming that there's other things you want to do in the loop. If not, then just

    print "@EMP_INFO"; # or print join "\n", @EMP_INFO;

    May be enough

Re: reading input
by b10m (Vicar) on Feb 20, 2004 at 21:13 UTC

    You could use Term::ReadKey too (something I always do). Sample:

    use strict; use Term::ReadKey; use Data::Dumper; my @EMP_INFO; ReadMode('normal'); print "What is your name: "; push @EMP_INFO, ReadLine(0); print "What is the location name: "; push @EMP_INFO, ReadLine(0); print "What is the age: "; push @EMP_INFO, ReadLine(0); foreach (@EMP_INFO) { chomp; print $_ ."\n"; }

    Please note that it's quite useles to chomp something and adding "\n" again, but this shows that if you are using this, and not directly printing the contents of @EMP_INFO, you may want to chomp the contents :)

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: reading input
by Skeeve (Parson) on Feb 20, 2004 at 21:09 UTC
    !#/usr/local/bin/perl
    #! not !#
    my @EMP_INFO; print "What is your name \n"; my $input_name = <STDIN>; push @EMP_INFO, <STDIN>;
    This will store the first line of input in $input_name and all the rest in @IMP_INFO. Same for all other input lines.

    Try
    my $input_name = <STDIN>; push @EMP_INFO, $input_name; print "What is the location name\n"; my $input_location = <STDIN>; push @EMP_INFO, <STDIN>; #correct this on your own print "What is the age \n"; my $input_age = <STDIN>; push @EMP_INFO, <STDIN>;
    The following will print out the complete array 3 times, if you corrected the code.
    for ( my $x=0; $x < @EMP_INFO; $x++ ) { # This will run 3 times.... print @EMP_INFO; # This will print out your whole array. Three times }
Re: reading input
by NetWallah (Canon) on Feb 20, 2004 at 21:51 UTC
    duff's code is cool, but, as usual, TIMTOWTDI:
    my @x; for (qw(Name Location Age)){ print qq(What is your $_ ?\n); push @x, scalar <STDIN>; #This will include a trailing '\n' in each element.. }; print qq(ANSWERS:\n@x\n);

    "Experience is a wonderful thing. It enables you to recognize a mistake when you make it again."