STDIN-perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting STDIN-perl
# 1  
Old 05-04-2010
STDIN-perl

Good morning!

How do I make the info that someone inputs from @userArray = <STDIN>, a new array?

Code:
@userArray = <STDIN>;
while ()
{
        chomp;
        last if ! /\d/;
        push(@userArray,$_);
}
my($sum,$avg) = &sumIt(@userArray);
print "Total:$sum\nAverage:$avg\n";

Im trying to capture what the user is inputing in the <STDIN>.

Any help would be greatly appreciated!

Ben

Last edited by Scott; 05-04-2010 at 03:59 AM.. Reason: Fixed code tags
# 2  
Old 05-04-2010
Quote:
Originally Posted by bigben1220
...
How do I make the info that someone inputs from @userArray = <STDIN>, a new array?

Code:
@userArray = <STDIN>;
while ()
{
        chomp;
        last if ! /\d/;
        push(@userArray,$_);
}
my($sum,$avg) = &sumIt(@userArray);
print "Total:$sum\nAverage:$avg\n";

Im trying to capture what the user is inputing in the <STDIN>.
...
Not sure what exactly you are looking for, but does this help ?

Code:
$
$
$ perl -lne 'push @x, $_;
             END {print "The input array is as follows:"; foreach (@x){print}
                  print "Sum     = ", eval(join("+",@x));
                  print "Average = ", eval(join("+",@x))/($#x+1);
                 }'
10
20
30
40
^Z
The input array is as follows:
10
20
30
40
Sum     = 100
Average = 25
$
$

The numbers in blue are input by the user at the command-line.
The text in red is a "Control+Z" and then the "Return" key, and it is an indication to the Perl program that the input has been terminated.
The text in green is printed by the Perl program.

tyler_durden
# 3  
Old 05-05-2010
Perfect!! Ok to switch it up and learn...

what is I wanted to divide the user input by 2?? THis is my code below...

I know /2 means divide by 2 correct?

[code]
print "The input array divided by 2 is as follows:\n"; foreach (@userArray)/2 {print}
my($sum,$avg) = &sumIt(@userArray);
print "Total:$sum\nAverage:$avg\n";
[code1]

thanks
ben
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

STDIN and STDOUT

Hallo, i have a script like: if ;then echo "OK" else echo "ERROR $2 is missing" fi; if ;then touch $2 fi; if ;then cat $1 | grep xy > $2 (1 Reply)
Discussion started by: eightball
1 Replies

2. UNIX and Linux Applications

Apache+Perl: interruption of STDIN

Hello dear community! I have faced with a trouble which I trying to solve for a long time. I have service of video streaming. And I have web and video roles for servers. Video servers upload video using SWFUpload and then stream it. Issue became during uploading of video. In random time... (3 Replies)
Discussion started by: sevmax
3 Replies

3. Shell Programming and Scripting

Redirecting stdin from fd 3-9?

Hi I'm trying to do something on the bash command line that I will later put into a bash shell script. I'm trying to take a program that reads stdin (using getline) and be able to keep it running in the background and fire "commands" to it. So what I thought I should do was to try taking... (3 Replies)
Discussion started by: niceguyeddie
3 Replies

4. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

5. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

6. Shell Programming and Scripting

Redirecting to stdin

Hi, I'm having trouble with my script. I have to select different choices without any interaction from a menu that looks like : a - xxxxxxxxxxxxxx b - xxxxxxxxxxxxxx c - xxxxxxxxxxxxxx d - xxxxxxxxxxxxxx I tried things like : echo "a" >&0 read < echo "a" but none worked. Any... (4 Replies)
Discussion started by: flame_eagle
4 Replies

7. Shell Programming and Scripting

redirect STDIN

can you redirect STDIN with command arguments? I have tried this approach: # ./script -option <argument1> <argument2> 0<$2 # $2: ambiguous redirect Is this possible? (4 Replies)
Discussion started by: prkfriryce
4 Replies

8. Shell Programming and Scripting

echo with stdin

Why doesnt echo output the contents of the file abc.txt as shown below ? chid@athena:~$ cat abc.txt sssss chid@athena:~$ echo < abc.txt chid@athena:~$ (6 Replies)
Discussion started by: systemsb
6 Replies

9. Shell Programming and Scripting

redirection stdin

hello all, I need to create a password change utility for a database. I need to gather at the command line the username, password and database sid. I have the program currently doing this. What I would like to do is not have the new password appear on the screen when I do my read command.... (2 Replies)
Discussion started by: whited05
2 Replies

10. Programming

stdin

hi, how does a program know whether some data are available from stdin? I would like to make a program which could read its data from stdin and _if_there_is_nothing_at_stdin_ from a file which name is given as an argument. If there is nothing in stdin and no filename is given as argument,... (2 Replies)
Discussion started by: marquis
2 Replies
Login or Register to Ask a Question