Hi All,
I new to perl scripting, trying to write a program to get multiple inputs from the users and it should be stored in one variable. can some one help me with it .
here is the sample code i tried , but its not working.
[root@u perl]# cat array.pl
#!/usr/bin/perl
print "enter the total no of numbers you want to enter";
chop($no=<STDIN>);
my @array=($no);
print "enter the numbers to array with one space gap";
@array=<STDIN>;
print "@array";
[root@u perl]#
Hi,
Not sure,what you are trying to do. May be this helps ?
Is it ok to provide the inputs in command line args. you can use default array @ARGV similar to default variable $_ instead of temporary arrays .
cat try.pl
#!/usr/bin/perl -w
foreach (@ARGV) {
print "Args are: $_\n";
}
Run as:
perl try.pl Num1 Num2 Num3
Output:
Args are: Num1
Args are: Num2
Args are: Num 3
Another try within quotes it is considered as a single argument :
perl try.pl "Num1 Num2 Num3"
Output:
Args are: Num1 Num2 Num3
Hi
Thanks for reply , I need to get input after the program starts . I have to get the input numbers from user in any series and need to identify missing number in the series .
Ex:
Please enter number of nos in the series .
5
Enter the numbers :
1
2
4
5
Here our script should identify the missing number in the series : "3"
And the output should be .
1
2
3
4
5
Try this:
#!/usr/bin/perl -w
my ($i,@a,@b);
print "Enter the number of numbers:\n";
chomp(my $tot=<STDIN>);
print "Enter the numbers:";
foreach (<STDIN>) {
chomp($_);
#if (! $_ =~ /^[0-9]$/ ) {
#print "Give number only as a input\n";
#exit;
#}
push @a,$_;
}
for ($i=1; $i<=$tot; $i++) {
push @a,$i if not $i ~~ @a;
}
@b=sort { $a <=> $b } @a;
print "output is:\n";
foreach (@b) {
print "$_\n";
}
Enter the number of numbers:
4
Enter the numbers:
1
2
4
output :
1
2
3
4
if you want to give input as numbers only , you can remove # from the lines starting with # from if condition.
otherwise, you get this warning message "Argument "" isn't numeric in sort at try.pl line 23, <STDIN> line ."
you can ignore this.
Hi Greet,
Thanks for update Its working now ..