Perl -- code


 
Thread Tools Search this Thread
Top Forums Programming Perl -- code
# 1  
Old 11-04-2013
Perl -- code

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.

Code:
[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]#

# 2  
Old 11-04-2013
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
Code:
#!/usr/bin/perl -w
foreach (@ARGV) {
print "Args are: $_\n";
}

Run as:
Code:
perl try.pl Num1 Num2 Num3

Output:
Code:
Args are: Num1
Args are: Num2
Args are: Num 3

Another try within quotes it is considered as a single argument :
Code:
perl try.pl "Num1 Num2 Num3"

Output:
Code:
Args are: Num1 Num2 Num3

# 3  
Old 11-04-2013
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:
Code:
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 .
Code:
1
2
3
4
5


Last edited by Scott; 11-04-2013 at 07:20 AM.. Reason: Code tags
# 4  
Old 11-04-2013
Try this:

Code:
#!/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";
}

Code:
Enter the number of numbers:
4
Enter the numbers:
1
2
4

output :
Code:
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.
# 5  
Old 11-05-2013
Hi Greet,

Thanks for update Its working now ..

Last edited by nanduri; 11-05-2013 at 10:23 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl code help

i'm using the below perl code to get a list of ips: my @allipsfound ; for my $elem (@gatches) { my ($ip) = split ":",$elem; print "$ip \n"; } it spits out several lines of ips. most of those ips are the same. how do i do sort and uniq in the above code to make it so that it only... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

perl code

What is the meaning of below code, which is capturing from one file . $runDate = $1 if $str =~ m/,"TransactTime":"({10})/; # get transaction date as run date Is it the date from the first line of file or last line of file??? (Note:Each line from the file contains date) (2 Replies)
Discussion started by: aish11
2 Replies

3. Shell Programming and Scripting

perl code

Perl code to get the diff of the two baselines in clearcase ? (2 Replies)
Discussion started by: saku
2 Replies

4. Shell Programming and Scripting

perl code

Hi Please read the below information carefully and help me to solve the issue . I want to know ,how to catch the word before perticular word using perl code?? Eg: if the row contains =========================== Original Order Tuple Duplicatel Execution Tuple ... (1 Reply)
Discussion started by: pspriyanka
1 Replies

5. Programming

Perl- How to catch the file in perl code?

Hi, plz see the below code , i have catch the file "Orders.20110714.out "file as a Orders*.out. but it giving me an error .it does not open the file. if the same thing i have done by code code-> ls Orders*.out then it gives me the output Orders.20110714.out i am trying apply the... (1 Reply)
Discussion started by: pspriyanka
1 Replies

6. Shell Programming and Scripting

perl: a way to see a sub code in debug mode: perl -de 0 ?

Is there a way to see or print a sub code? Sometime a sub could be already defined, but in the debug mode (so, interactively) it could be already out of screen. So, I would think about a way to check if the sub is defined (just 'defined' is not a problem) and how it is defined. Also, if... (4 Replies)
Discussion started by: alex_5161
4 Replies

7. Shell Programming and Scripting

PERL Code

I have a code block ... $cmd = "find $audit_dir -mtime +$days_to_keep -exec rm {} \\;"; unless(open(CMD, "$cmd |")){ $msg = "Error command : $cmd \n\n"; print_log $lgh,"$msg",1; exit 1; } print_log $lgh, "Deleting... (2 Replies)
Discussion started by: talashil
2 Replies

8. Shell Programming and Scripting

need help to write perl code

I have 2 files , one is master file and another file i am genearting which contains Number of fails for all host & version. The first file is master file which contains 2 column, Test Case name and Product Name: daily/DB/attach/attach_redundancy.exp: YRS daily/DB/help/help.exp: ... (0 Replies)
Discussion started by: getdpg
0 Replies

9. UNIX for Dummies Questions & Answers

perl code help

if anyone knows how to do the loop and the function listed, please let me know it would be a great help. Thanks! •Create an array NUM with the following ten values in it: 14, 25.6, 43, 22, 412.14, 819, 2, 721, 7.218, 11.9 •Print the array NUM •Create an array COLOR with the following... (1 Reply)
Discussion started by: circleW
1 Replies

10. Shell Programming and Scripting

Perl running C code

Ok, I am thinking about developing a small little web app to help teach C to people. So you can type some C code into a text area, then a CGI Perl script or something similar would compile the code, then possibly execute it showing the results in another text area. My concern of course is... (1 Reply)
Discussion started by: gdboling
1 Replies
Login or Register to Ask a Question