LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2011, 02:52 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
perl stdin to array


I want to load an array from the command line
which is pretty easy

@array = <STDIN>

however you have to hit ctrl - d to get out of it.
none of the users get that.

I was trying to write something that would stop loading the array once a blank line was entered

I came up with this

@array = <STDIN>
LINE: while (<STDIN>) {last LINE if /^$/; }
print "@array";

which does not work - I still have to hit ctrl-d to get to the print array part.

i just want the array to stop loading upon entering a blank line.
 
Old 05-20-2011, 03:45 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This works if the user is prompted to write all the elements of the array onto one line. Pressing enter terminates the input:
Code:
my @arr = split(/\s+/, <>);
foreach (@arr) {
  printf "%s\n", $_;
}
 
Old 05-20-2011, 04:10 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
For a multiline input, if the user leaves a blank line to terminate, use a loop instead:
Code:
my @arr = ();
while (<STDIN>) {
  chomp($_);
  last if ($_ eq '');
  push(@arr, $_);
}

foreach (@arr) {
  printf "%s\n", $_;
}

printf "Number of elements in \@arr: %d\n", scalar(@arr);
The chomp function strips the trailing newline from the input, before pushing it into the array.


Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

Last edited by colucix; 05-20-2011 at 04:11 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Read from stdin in perl, do not wait for EOF smeezekitty Programming 1 04-28-2010 03:17 PM
Apache+Perl: interruption of STDIN sevmax Linux - Server 0 10-13-2009 03:42 AM
Help with perl script using mysql and stdin. motdman Programming 11 07-27-2009 01:22 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
How to Encrypt <STDIN> in PERL 5.8 SPo2 Programming 2 01-21-2006 12:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration