By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

split function

P: 8
Hi,

I am new to perl programming.

I have a requirement to validate the input received from the user with the file and send it to the printer.

For Eg:
script -o option -p printername -f filename

script - is the name of my script
-o option- is the option that has to be validated with another file
-p printername - is the name of my printer
-f filename - is the filename which has to be printed.

how to use this split function in this case. I want to take" -o option" and compare it with the file.
I already have a print queue ready. what is teh command to send the filename to the printer.

Please help me.

Thanks.
Nov 10 '08 #1
Share this Question
Share on Google+
13 Replies


nithinpes
Expert 100+
P: 410
To process command line options, you can make use of GetOpt::Std module that comes with default Perl installation.
Nov 10 '08 #2

P: 8
i had used the below command to check whether the file exists or not.
eventhough the file exists in proper path, it is going to else part.

or if the path is wrong, it is not dying....
Please help...
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. my $file = <STDIN>;
  6. print ( $file );
  7. $ENV{'PPD'} = $file;
  8.  
  9. # file
  10.  
  11. open (FILE, $file) || die "cannot open";
  12.  
  13. if (-e $file) {
  14. print "exists \n";
  15. }
  16. else
  17. {
  18. print ( $file );
  19. print "not \n";
  20. }
  21. close(FILE);
  22.  
Nov 11 '08 #3

nithinpes
Expert 100+
P: 410
i had used the below command to check whether the file exists or not.
eventhough the file exists in proper path, it is going to else part.

or if the path is wrong, it is not dying....
Please help...
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. my $file = <STDIN>;
  6. print ( $file );
  7. $ENV{'PPD'} = $file;
  8.  
  9. # file
  10.  
  11. open (FILE, $file) || die "cannot open";
  12.  
  13. if (-e $file) {
  14. print "exists \n";
  15. }
  16. else
  17. {
  18. print ( $file );
  19. print "not \n";
  20. }
  21. close(FILE);
Do remember to chop the newline while taking input from command line/terminal. That will solve the issue.
Expand|Select|Wrap|Line Numbers
  1. chomp(my $file = <STDIN>);
  2.  
Nov 11 '08 #4

P: 8
Hi,

Thanks for the reply..

i have got the option from the user Eg: -o xxx=yyy

@options=@ARGV[0..$#ARGV];

I want to ignore -o and take only xxx in one variable and yyy in another variable..

Please help me with the commands..
Thank You.
Nov 11 '08 #5

nithinpes
Expert 100+
P: 410
If you are calling the script like,
perl script.pl -o xxx=yyy

use:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. my $option=$ARGV[1];     # get second argument
  3. my ($first, $second) = split /=/,$option;   # split across '='
  4. print "$first\n$second\n";
  5.  

However, if you are handling multiple options, it is better to use Getopt::Std as I said before.

- Nithin
Nov 11 '08 #6

P: 8
Hi Nithin,

Thank you for the reply.

this is what i exactly wanted.

perl script.pl -o xxx=yyy -o aaa=bbb -o asd=asd ........

i want to execute my script in this way...

so using getopt, returns only the last value.
and also i want to ignore -o and use only xxx=yyy, aaa=bbb, asd=asd values.
xxx,aaa,asd separately in a array and yyy,bbb,asd separately in a array......

please help me if you have any idea on this.

Thanks in advance...
Nov 11 '08 #7

nithinpes
Expert 100+
P: 410
Hi Nithin,

Thank you for the reply.

this is what i exactly wanted.

perl script.pl -o xxx=yyy -o aaa=bbb -o asd=asd ........

i want to execute my script in this way...

so using getopt, returns only the last value.
and also i want to ignore -o and use only xxx=yyy, aaa=bbb, asd=asd values.
xxx,aaa,asd separately in a array and yyy,bbb,asd separately in a array......

please help me if you have any idea on this.

Thanks in advance...
Try:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my @options=grep(!/-o/,@ARGV); # get all args other than -o
  4. my (@first,@second);
  5. foreach(@options) {
  6. my ($first, $second) = split /=/;
  7. push @first,$first;
  8. push @second,$second;
  9. }
  10.  
Nov 11 '08 #8

P: 8
Thanks for this valuable information.
If there is any rating for the replies I would like to give you 10.
Thanks a lot.
Nov 18 '08 #9

P: 8
I am executing the below script in the terminal.

/usr/lib/filter/rip "<some options>" /anyfilename > /outputfile

I am executing a script named rip with some options and giving the input file and redirecting it to a file.

I want to use perl script to execute this command.

I used system command as below.

system("/usr/lib/filter/rip "<some options>" /anyfilename > /outputfile");

It is not working. How to execute.
Please help.
Nov 18 '08 #10

P: 8
I have executed the below command and it is partially working.

@pr = ("/usr/lib/rip","option","/test",">","/del");
print ( "@pr" );
system("@pr");

Bash#:/usr/lib/rip option /test > /del

This <option> has to be displayed in double quotes ("option").
But this double quotes are taken as space.

The command I want to execute is as below.

Bash#:/usr/lib/rip "option" /test > /del

Please help.
Nov 19 '08 #11

nithinpes
Expert 100+
P: 410
I have executed the below command and it is partially working.

@pr = ("/usr/lib/rip","option","/test",">","/del");
print ( "@pr" );
system("@pr");

Bash#:/usr/lib/rip option /test > /del

This <option> has to be displayed in double quotes ("option").
But this double quotes are taken as space.

The command I want to execute is as below.

Bash#:/usr/lib/rip "option" /test > /del

Please help.
The double quotes around options should be escaped. For ex., consider the command you need is
Expand|Select|Wrap|Line Numbers
  1. #path/rip "Hello man" input.txt  > output.txt
  2.  
Then, the system command should be as below:
Expand|Select|Wrap|Line Numbers
  1. system("path/rip \"Hello man\" input.txt > output.txt");
  2.  
Nov 19 '08 #12

P: 8
Thanks for the reply.

I want to store the output file. If i store it in an $var or @array, the file is not saved properly.

my script:
my $file = "/tmp/input";
my @pr = "/tmp/file.log";

@command = ("/usr/lib/rip \"options\" $file > @pr");
system ( "@command" );

file.log shows some file size. When i try to open it, it is not responding.
please let me know how to solve this issue.
This issue is not present wen a txt file is given as input. other than text file all other files are giving problem.

Thanks,
Nov 19 '08 #13

KevinADC
Expert 2.5K+
P: 4,059
Expand|Select|Wrap|Line Numbers
  1. my $file = '/tmp/input';
  2. my $out = '/tmp/file.log';
  3.  
  4. my $command = ("/usr/lib/rip \"options\" $file > $out");
  5. system ($command);
Nov 19 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.