Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Reading multi line input from user
I wish to read input from a user that has multiple lines with carriage
returns from <stdin> but not actually stop reading until a single # on a
line by itself. Is there a module / package / function that will aid in
this? I would like to do something like below:



Until(<STDIN> = "#") {

$incoming_lines = <STDIN>;

}

$storage_var = $incoming_lines;





This is most like NOT working code, it is a logic example so that you
might understand what I am trying to accomplish, so please no comments on
the actual code shown.



Chris Hood



Report this thread to moderator Post Follow-up to this message
Old Post
Christopher L Hood
04-25-05 08:56 PM


RE: Reading multi line input from user
christopher.l.hood@verizon.com wrote:
> I wish to read input from a user that has multiple lines with carriage
> returns from <stdin> but not actually stop reading until a single #
> on a line by itself.

You can use perl's $/ variable to define an record separator. The default is
"\n", but you can change it.

Try this:

$/ = "\n#\n";

while (<STDIN> ) {
chomp;
print "You entered: $_\n";
}

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Showalter
04-25-05 08:56 PM


Re: Reading multi line input from user
On 4/25/05, christopher.l.hood@verizon.com
<christopher.l.hood@verizon.com> wrote:
> I wish to read input from a user that has multiple lines with carriage
> returns from <stdin> but not actually stop reading until a single # on a
> line by itself. Is there a module / package / function that will aid in
> this? I would like to do something like below:
>=20
> Until(<STDIN> =3D "#") {
>=20
>             $incoming_lines =3D <STDIN>;
>=20
> }
>=20
> $storage_var =3D $incoming_lines;
>=20
> This is most like NOT working code, it is a logic example so that you
> might understand what I am trying to accomplish, so please no comments on
> the actual code shown.
>=20
> Chris Hood
>=20
>=20

Chris,

It really depends on what you want to do and how you want to get your input=
.

my @input =3D <STDIN>;
chomp @input;

is probably the simplest.  It relys on your OS to supply EOF and
terminate stdin.  On unix systems, that usually means pressinf Ctrl-D.

my @input;
while (<STDIN> ) {
chomp;
push @input, $_;
}

Is another option.  This lets you parse the text as it's entered, so
you can easily do things like create a custom EOF:

while (<STDIN> ) {
chomp;
last if /^#$/;
push @input, $_;
}

You could also replace that push with:

$input .=3D " $_";

or something similar if you desperately wanted a scalar for some reason.

And finally, you could change the value of $/.  Just make sure you've
properly localized it, and no other files will be read in the same
scope.  Something like:

sub get_input {
local $/ =3D "\n#\n";
my $input =3D <STDIN>;
return $input
}

will work fine, but you can get into a lot of trouble very quickly by
doing something like:

$/ =3D "\n#\n";
my $input =3D <STDIN>;
open(FH, "<", "my/foo.txt");
while (<FH> ) {
do something if ((/something/) and ($input =3D~ /something else/));
}

HTH,

--jay

Report this thread to moderator Post Follow-up to this message
Old Post
Jay Savage
04-26-05 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:34 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.