Hello Anant,
> i want to input some numbers via <stdin> in while loop.And loop should be
> broken if any nonnumeric character is entered.So how it can be checked.
>
> ........................
> .........................
> my @ln;
> my $i=0;
> print"Give line numbers you want to put into array.\n";
> while(1){
> $ln[$i]=<stdin>;
> chomp $ln[$i];
> if($ln[$i] =~ /\D/){
> print"Its not numeric value.\n";
> $i--;
> last;
> }
> $i++;
> }
> .............................
In the above program, `$i` is mostly useless. The length of the array can
easily be found out by using `scalar @ln`; Perl's `push` function can be used
for pushing values into the array. There is no need for an index. The program
can be rewritten as:
use strict;
use warnings;
my @numbers;
print "Enter numbers:\n";
while (1) {
chomp( my $number = <STDIN> );
if ( $number =~ /\D/ ) {
print "$number is not a numeric value.\n";
last;
}
else {
push @numbers, $number;
}
}
> One more query:-
> HOW TO REMOVE ANY ELEMENT FROM AN ARRAY.
> I mean if i have allocated till arr[5]. Now I want to remove the value
> arr[4] and arr[5]. So how it can be done so that $#arr would tell 3.
You can use the `splice` function (read: `perldoc -f splice`) for this:
splice @array, 4, 2;
Regards,
Alan Haggai Alavi.
--
The difference makes the difference.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/