473,466 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

split function

8 New Member
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
13 1970
nithinpes
410 Recognized Expert Contributor
To process command line options, you can make use of GetOpt::Std module that comes with default Perl installation.
Nov 10 '08 #2
nivet
8 New Member
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
410 Recognized Expert Contributor
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
nivet
8 New Member
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
410 Recognized Expert Contributor
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
nivet
8 New Member
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
410 Recognized Expert Contributor
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
nivet
8 New Member
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
nivet
8 New Member
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
nivet
8 New Member
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
410 Recognized Expert Contributor
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
nivet
8 New Member
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
4,059 Recognized Expert Specialist
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

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

Similar topics

5
by: Arjen | last post by:
Hi All, What I want to is using a string as PATTERN in a split function. This makes it possible for me to change the PATTERN on one place in my script... For example: $separator = ";"; $line...
2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
4
by: Henry Chen | last post by:
Hi, I have a string that needs to be parsed into the string. The separator is not char. It is something like " at ". With current string.Split function, it doesn't work. Is there any exist...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
10
by: mb | last post by:
I was wondering if there is an easy, more useful Split function that will split with a string delimiter like "<>" or "////"?
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: sck10 | last post by:
Hello, I have a list of email addresses that I need to send email to from the website. I am trying to use the "Split" function to get all the To's and then use the uBound function for the...
7
by: Jordi Rico | last post by:
Hi, I know I can split a string into an array doing this: Dim s As String()=Regex.Split("One-Two-Three","-") So I would have: s(0)="One" s(1)="Two"
1
by: John | last post by:
Hi I have written a Split function which in turn calls the standard string split function. Code is below; Function Split1(ByVal Expression As String, Optional ByVal Delimiter As String = " ",...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.