CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
insert numbers in array using standard input

 



jazzo
Novice

Aug 19, 2012, 1:34 PM

Post #1 of 14 (3395 views)
insert numbers in array using standard input Can't Post

Hi there I am trying to store some numbers (5) in an array using the STDIN, but I am not quite sure how to achieve that.
Here's my code so far:



Code
#!/usr/bin/perl 
#array2.plx
use warnings;
use strict;
print "Here's an array: \n";
my @array = qw|first second third fourth|;
print "@array \nInsert 5 numbers in the array";

my @array2;

The array2 is the array that will hold the numbers. Now, do I need to loop through the STDIN to get 5 numbers?
DO I need to enclose the STDIN in the loop?
I'd appreciate some advice, I wouldn't want the code because I would like to try to do it myself
thanks


Laurent_R
Veteran / Moderator

Aug 19, 2012, 2:01 PM

Post #2 of 14 (3392 views)
Re: [jazzo] insert numbers in array using standard input [In reply to] Can't Post

Hi,

you are taking it the wrong way, if I understand what you want correctly.

in:


Code
my @array = qw|first second third fourth|;


you are giving values to the first fourth elements of your array. It is unlikely that is what you are rally trying to do. (BTW, "first" will be the value of $array[O], "second the value of $array[1], and so on. This is also presumably not what you want.

Now, you have basically two ways of handling your problem.

You can loop through the array, and, for each element of this array, ask your user what she or he wants for a value. Or you want your user to enter a string aith all the value, for example something like "17, 13, 7, 9876, 1" (or possibly "17 13 7 9876 1", or whatever) and then assign each number with proper code to do it.

Both approaches are feasible and quite easy. It is up to you to decide on how vou want to do it. Obviously, the coding will be different depending on how you expect your user to enter the data.


BillKSmith
Veteran

Aug 20, 2012, 11:49 AM

Post #3 of 14 (3365 views)
Re: [jazzo] insert numbers in array using standard input [In reply to] Can't Post

It is easy to get all five number at once.

Code
#!/usr/bin/perl 
#array2.plx
use warnings;
use strict;
my @array = qw|first second third fourth fifth|;
print join( ' ', @array), "\n";
print "Type five number separated by spaces. Terminate with 'Enter' key\n\n";
my $response = <>;
my @array2 = split /\s/, $response;
if (@array2 != 5) {
die "Invalid input\n";
}
print "\n";
foreach (@array2) {
print;
print "\n";
}

Good Luck,
Bill


jazzo
Novice

Aug 20, 2012, 1:18 PM

Post #4 of 14 (3362 views)
Re: [BillKSmith] insert numbers in array using standard input [In reply to] Can't Post

Hello BillKSmith ,
thanks for your code but I am at the very beginning of my perl leanrning experience, so there are quite a few things I am not too sure I understand in your code. Would you be able to give me a hand with that please?
Before doing that, I think my code was a bit confusing. The first array

Code
  @array

isn't really part of what I was trying to achieve (save the values input by the user in an array) it is just an array I created holding 4 values, that's all : - )
1)join method. I had a look at it also online, but I am not sure I understand exactly what it does.

Code
 print join( ' ', @array), "\n";

. Not quite sure what we join with this sorry (I looked at this http://www.tizag.com/perlT/perlarrays.php but still not clear).
Now let's get to what I am triying to achieve.
this assignment:

Code
my $response = <>;

, what does it do? What do the angle brackets signify?
The split function usualy is used to convert a string to an array..but how about this?

Code
my @array2 = split /\s/, $response;

here

Code
 if (@array2 != 5) {

are we saying if the array hasn't 5 numbers?

hope you can help me thanks.

@Laurent_R
As said above the first array doesn't have anything to do with what I was trying to achieve, sorry for the confusion.
So back to the econd array array2 which is the one that will contain the numbers input by the users. DO you mean something like this?


Code
#!/usr/bin/perl 
#array2.plx
use warnings;
use strict;
print "Here's an array: \n";
my @array = qw|first second third fourth|;
print "@array \nInsert 5 numbers in the array";

my @array2;

for (@array2){
@array2 = <STDIN>
}
print "@array";


thanks


Laurent_R
Veteran / Moderator

Aug 20, 2012, 1:48 PM

Post #5 of 14 (3359 views)
Re: [jazzo] insert numbers in array using standard input [In reply to] Can't Post

A couple of answers.

print join( ' ', @array), "\n";

This transforms an array into a string, adding a space between each array element to make it more readable.

my $response = <>;

Expects input from the user. Input will be sored in the $response variable. The angle brackets mean that an input is needed. When empty, this means standard input, i.e. the keyboard.

my @array2 = split /\s/, $response;

splits the user input string into an array of words (the splitting pattern is the space, so it basically splits a sentence into words.


Code
 if (@array2 != 5) {


are we saying if the array hasn't 5 numbers?

Yes, that's basically what the code is saying.


Laurent_R
Veteran / Moderator

Aug 20, 2012, 1:57 PM

Post #6 of 14 (3358 views)
Re: [Laurent_R] insert numbers in array using standard input [In reply to] Can't Post

Hmm.


Code
my @array2;  

for (@array2){
@array2 = <STDIN>
}
print "@array";


doesn't do you any good, because @array2 is not defined before you start.

So, the for statement does not bring you any additional information. You are basically looping on an empty array.


BillKSmith
Veteran

Aug 20, 2012, 3:47 PM

Post #7 of 14 (3351 views)
Re: [Laurent_R] insert numbers in array using standard input [In reply to] Can't Post

I think that Laurent has already answered your questions about my code, but I will try to put it all together.


Code
!/usr/bin/perl 
#array2.plx

Copied from your code.

Code
use warnings; 
use strict;

Prohibit symbolic reference and default global variables and provide useful error messages.

Code
my @array = qw|first second third fourth fifth|; 
print join( ' ', @array), "\n";

Store one ordinal in each element of @array them print that array. The join is used to add a space between words.

Code
print "Type five number separated by spaces.  Terminate with 'Enter' key\n\n"; 
my $response = <>;

Prompt the user to enter five numbers. Store his answer in the scalar $response.

Code
my @array2 = split /\s/, $response;

Parse his response by splitting it on whitespace. Store each number in an element of @array2.

Code
if (@array2 != 5) { 
die "Invalid input\n";
}

If the user did not follow instructions and enter exactly five numbers, his input is invalid. Abort with a message that tells him so. (In production code, we would also verify that all his fields are in fact numbers.)

Code
print "\n"; 
foreach (@array2) {
print;
print "\n";
}

Print the input data, one number per line. (I probably should have used join for this array also.)
Good Luck,
Bill


FishMonger
Veteran / Moderator

Aug 20, 2012, 4:01 PM

Post #8 of 14 (3350 views)
Re: [BillKSmith] insert numbers in array using standard input [In reply to] Can't Post


Quote
print join( ' ', @array), "\n";


As long as you haven't changed the default field separator, the idiomatic way to write that is:

Code
print "@array\n";



jazzo
Novice

Aug 21, 2012, 1:15 PM

Post #9 of 14 (3319 views)
Re: [FishMonger] insert numbers in array using standard input [In reply to] Can't Post

fantastic thanks. One question about this

Code
if (@array2 != 5)

With this line we are not checking the length of the array but the number of elements, correct?


Laurent_R
Veteran / Moderator

Aug 21, 2012, 1:31 PM

Post #10 of 14 (3317 views)
Re: [jazzo] insert numbers in array using standard input [In reply to] Can't Post

Yes, correct. Although, to tell the truth, the length of an array is usually considered to be the number of its elements.


jazzo
Novice

Aug 21, 2012, 1:36 PM

Post #11 of 14 (3315 views)
Re: [Laurent_R] insert numbers in array using standard input [In reply to] Can't Post

hi thanks, I thought checking the length would have been differet, in that the lenght is always the elements number -1 isn't it?


FishMonger
Veteran / Moderator

Aug 21, 2012, 1:55 PM

Post #12 of 14 (3309 views)
Re: [jazzo] insert numbers in array using standard input [In reply to] Can't Post

-1 is used when specifying/accessing the last element in the array. It has nothing to do with the length (or size) of the array.


Code
my @array = qw|first second third fourth fifth|; 

print $array[-1]; # prints fifth
print $array[-2]; # prints fourth
print $array[-3]; # prints third



(This post was edited by FishMonger on Aug 21, 2012, 1:55 PM)


Laurent_R
Veteran / Moderator

Aug 21, 2012, 1:58 PM

Post #13 of 14 (3307 views)
Re: [jazzo] insert numbers in array using standard input [In reply to] Can't Post

Oh, well, of course, you are right.

Since Perl arrays start at $array[0], they end up at $array[$max -1].

But this is not necessarily the way you record your data.


jazzo
Novice

Aug 21, 2012, 2:12 PM

Post #14 of 14 (3306 views)
Re: [Laurent_R] insert numbers in array using standard input [In reply to] Can't Post

Ok fantastic thanks for all your help

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives