Re: Read two values from input
by jethro (Monsignor) on Nov 25, 2011 at 00:18 UTC
|
The code is not with error, it just doesn't work the way you want. Two reasons:
1) <STDIN> reads a complete line until you hit the carriage return key.
2) The line read in by <STDIN> still has that carriage return character at the end
So instead of reading two lines you obviously want to read one line and then separate the two values:
$line=<STDIN>;
chomp $line; # this removes the CR character at line end
($a,$b)= split / +/, $line; #splits the line wherever it finds spaces
| [reply] [d/l] |
|
|
split(' ', $line) has the advantage of ignoring leading whitespace and accepting tabs in addition to spaces. It also makes the chomp useless.
| [reply] [d/l] [select] |
|
|
The default split is /\s+/;. This / +/ is a bit bizarre.
The only difference between the default split (/\s+/, $_ ) and split (' ', $_ ) is that in the first case a leading space will generate a null field. Whereas in the second case, this is suppressed.
The split on ' ', is a special case and although there is just a space there, it means any of the five white space characters: \f\t\r\n\0x40(space). Because both forms operate on all 5 white space characters, any trailing white space will be ignored in both syntax's (unless there is a specified field limit).
There are a lot of tricky special case things about Perl syntax.
| [reply] |
|
|
s/There are a lot of tricky special case things about Perl syntax. /There are a lot of context dependent features in Perl, it has a rich (and sometimes ugly) syntax./
| [reply] |
|
|
Thanks jethro...
And every one who visited for help....Thanku
| [reply] |
Re: Read two values from input
by TJPride (Pilgrim) on Nov 25, 2011 at 12:12 UTC
|
use strict;
use warnings;
my $inp = getInput('Enter two values');
$inp = [split /[\s,]+/, $inp];
print "Values entered are: $inp->[0], $inp->[1]\n";
sub getInput {
my $inp;
while (1) {
print "$_[0]: ";
chomp($inp = <STDIN>);
return $inp if $inp;
}
}
| [reply] [d/l] |
|
|
TJP: Your split is highly commendable -- because you considered the possibility that a user, entering two numbers on a single line might separate them with a space; a comma; or both. ++.
However, you may have heard the (boastful) observation "Perl makes hard things, easy." It seems to me you've done the reverse (or at least, unnecessarily complicated the code) by introducing the sub.
But how about keeping the code simple:
perl -e "my $inp = <>;@input = split /[\s,]+/, $inp; print \"$input[0] : $input[1]\";"
| [reply] [d/l] |
|
|
| [reply] [d/l] |
Re: Read two values from input
by ansh batra (Friar) on Nov 25, 2011 at 04:31 UTC
|
#! usr/bin/perl -w
use strict;
print "enter two values\n";
my $x=<STDIN>;
chomp($x);
my $y=<STDIN>;
chomp($y);
print "two values are $x,$y\n";
output
enter two values
10
20
two values are 10,20
the first two lines of code are not compulsion here
see this | [reply] [d/l] [select] |
|
|
The original specification was to enter two numbers, "10 20", which seemed to be coming from the same line of input. Your solution works only if the two numbers come on separate lines of input, so it fails if the original spec. was accurate. Frankly, aside from chomp, you essentially presented the OP's solution back to him.
Only one line of input is to be read, and must then be split to multiple components, as demonstrated in other solutions earlier in the thread. Depending on how you wield split, chomp may or may not also be necessary. But it's really not the pivotal component of a correct solution.
You also linked to a page about using strict, which while being a "good idea", has nothing to do with why the OP's code wasn't doing what he expected.
| [reply] |
|
|
davido
i have written
"if you want to get the values in different variables directly"
and
"the first two lines of code are not compulsion here"
i think that explains that i know the user specifications
and used warning and strict because i have been told by many monks to do so
submitted the solution thinking that i would be beneficial for knils_r00t.
sorry if i am wrong
| [reply] |
|
|
Re: Read two values from input
by Marshall (Canon) on Nov 27, 2011 at 13:55 UTC
|
Depends upon what you want in terms of data validation. Your example shows two integer numbers, so that is what this does. Oh, BTW do not use $a or $b as variables in a Perl program as they have special meanings in sort().
I think this would have worked out better if I had only used regex, but my brain was on this "split" subject. But in any event, this shows how to detect too many arguments on the input line, what I just called "kruft".
#!/usr/bin/perl -w
use strict;
my $line;
my ($x,$y);
my $two_nums_valid =0;
while ( (!$two_nums_valid) and (print "enter 2 integer numbers: ")
and ($line =<STDIN>) )
{
next if $line =~ /^\s*$/; # just re-prompt on blank lines
chomp $line; #needed here to detect real $kruft
($x, $y, my $kruft) = split (' ', $line);
if (defined $kruft)
{
print "illegal format - only two integer numbers allowed!\n";
next;
}
if (!defined $y or $x !~ /^\d+$/ or $y !~ /^\d+$/)
{
print "two integer numbers are needed, like: 10 20\n";
next;
}
$two_nums_valid =1;
}
print "hooray the numbers are: $x and $y\n";
__END__
C:\TEMP>perl inputXY.pl
enter 2 integer numbers: a b
two integer numbers are needed, like: 10 20
enter 2 integer numbers: a b 10
illegal format - only two integer numbers allowed!
enter 2 integer numbers: a 10
two integer numbers are needed, like: 10 20
enter 2 integer numbers: 10 a 20
illegal format - only two integer numbers allowed!
enter 2 integer numbers: 2 2.5
two integer numbers are needed, like: 10 20
enter 2 integer numbers: 20
two integer numbers are needed, like: 10 20
enter 2 integer numbers: 20 b
two integer numbers are needed, like: 10 20
enter 2 integer numbers: 30 44
hooray the numbers are: 30 and 44
| [reply] [d/l] |
Re: Read two values from input
by Anonymous Monk on Nov 25, 2011 at 10:00 UTC
|
Nobody yet mentioned that you shouldn't be using $a and $b, as those two variables are reserved for use by the sort function.
| [reply] [d/l] [select] |
Re: Read two values from input
by pvaldes (Chaplain) on Nov 25, 2011 at 13:01 UTC
|
| [reply] [d/l] |