CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
comma seperated list as Command line arguement

 



Tejas
User

May 30, 2014, 7:31 AM

Post #1 of 9 (4561 views)
comma seperated list as Command line arguement Can't Post

Hi

I have tried running the code like below.

perl ./Tetst.pl -s (10,20,30) ,but this doesnt seem to work.
Is it possible to send a list as command line arguements

if ($opt_e)
{
$rcsl_etid=$opt_e;
}

Thanks
Tejas


Laurent_R
Veteran / Moderator

May 30, 2014, 7:46 AM

Post #2 of 9 (4557 views)
Re: [Tejas] comma seperated list as Command line arguement [In reply to] Can't Post

Sorry, I don't understand what you are trying to do.


Tejas
User

May 30, 2014, 8:05 AM

Post #3 of 9 (4553 views)
Re: [Laurent_R] comma seperated list as Command line arguement [In reply to] Can't Post

i would like to send commanline arguements with a single flag in the code.

perl ./Tetst.pl -s (10,20,30)

I cant use them as argv[0],... as i have some more command line arguements to send..

Using Getopts , -s flag , i would want to send a list .
These three values will be sent to db query in the prel script.

select * from table where id in (10,20,30) ;
,but this doesnt seem to work.


FishMonger
Veteran / Moderator

May 30, 2014, 8:27 AM

Post #4 of 9 (4550 views)
Re: [Tejas] comma seperated list as Command line arguement [In reply to] Can't Post

Use the Getopt::Long module. It supports options with multiple values, but it's an experimental feature which may go away in a future release.

This example defines an option that take 1 to 5 values and ignores additional values.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;

my @id;
GetOptions('id|s=i{1,5}' => \@id);

print Dumper \@id;


Test run:
D:\test>test.pl -s 10 5 8 9 11 66 33

Code
$VAR1 = [ 
10,
'5',
'8',
'9',
'11'
];


http://search.cpan.org/~jv/Getopt-Long-2.42/lib/Getopt/Long.pm#Options_with_multiple_values


(This post was edited by FishMonger on May 30, 2014, 8:28 AM)


Tejas
User

May 30, 2014, 10:02 AM

Post #5 of 9 (4520 views)
Re: [FishMonger] comma seperated list as Command line arguement [In reply to] Can't Post

Hi

This seems to work
But i would want to send all these vales as a part of sql query


my @id,@id1;
our ( $opt_w, $opt_x, $opt_s, $opt_a, $opt_o, $opt_d, $opt_e );
#getopts("awx:o:d:");
#getopts("awx:s:o:d:e:");
GetOptions('id|s=i{1,5}' => \@id ,'id1|e=i{1,5}' => \@id1);
#getopts("awx:o:d:");
#print Dumper \@id;
#print Dumper \@id1;
my $arrSize = @id;
my $arrSize1= @id1;
my ($etid1,$etid2,$etid3,$etid4) = @id;
my ($etid11,$etid12,$etid13,$etid14) = @id1;
#print "$arrSize,$arrSize1 \n" ;
print(" Option name is $etid1,$etid2,$etid3,$etid4 : $etid11,$etid12,$etid13,$etid14 ");
#print("$arrSize,Option name is $opt_name and value is $opt_value,$opt_value1,$opt_value2\n");



Can some one tell me why the values are getting printed and erased on screen...
This works ,
Thanks
Tejas


(This post was edited by Tejas on May 30, 2014, 10:44 AM)


Tejas
User

May 30, 2014, 10:27 AM

Post #6 of 9 (4506 views)
Re: [FishMonger] comma seperated list as Command line arguement [In reply to] Can't Post

And does this work
perl dbaccess_test -s 10 11 12 13 -e 40 50 60 70


Code
my @id; 
our ( $opt_w, $opt_x, $opt_s, $opt_a, $opt_o, $opt_d, $opt_e );
getopts("awx:o:d:");
#getopts("awx:s:o:d:e:");
GetOptions('etid|s=i{1,5}'=>\@id ,'id|e=i{1,5}' => \@id1);
print Dumper \@id;
print $id;
my $arrSize = @id;
my ($etid1,$etid2,$etid3,$etid4) = @id;
my ($etid11,$etid12,$etid13,$etid14) = @id1;
print("$arrSize Option name is $etid1,$etid2,$etid3,$etid4 : $etid11,$etid12,$etid13,$etid14 ");
#print("$arrSize,Option name is $opt_name and value is $opt_value,$opt_value1,$opt_value2\n");


Throws error
Unknown option: s
$VAR1 = [];


(This post was edited by Tejas on May 30, 2014, 10:29 AM)


FishMonger
Veteran / Moderator

May 30, 2014, 10:50 AM

Post #7 of 9 (4492 views)
Re: [Tejas] comma seperated list as Command line arguement [In reply to] Can't Post

Your code doesn't compile so it cant output that error.

Why are you declaring those vars as globals?


(This post was edited by FishMonger on May 30, 2014, 10:50 AM)


FishMonger
Veteran / Moderator

May 30, 2014, 10:52 AM

Post #8 of 9 (4489 views)
Re: [Tejas] comma seperated list as Command line arguement [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;

my (@id, @id1);
GetOptions('etid|s=i{1,5}'=>\@id ,'id|e=i{1,5}' => \@id1);

print Dumper \@id, \@id1;



Code
D:\test>test.pl -s 10 11 12 13 -e 40 50 60 70 
$VAR1 = [
10,
'11',
'12',
'13'
];
$VAR2 = [
40,
'50',
'60',
'70'
];



Tejas
User

May 30, 2014, 10:56 AM

Post #9 of 9 (4486 views)
Re: [FishMonger] comma seperated list as Command line arguement [In reply to] Can't Post

Hi

Finally Found a way here

use Data::Dumper;
my $start_run = time();


my @id,@id1;
our ( $opt_w, $opt_x, $opt_s, $opt_a, $opt_o, $opt_d, $opt_e );
#getopts("awx:o:d:");
#getopts("awx:s:o:d:e:");
GetOptions('s=i{1,5}' => \@id ,'e=i{1,5}' => \@id1,'fd=s' => \$fd , 'ld=s' => \$ld);

#getopts("awx:o:d:");
#print Dumper \@id;
#print Dumper \@id1;
my $arrSize = @id1;
my $arrSize1= @id;
my ($etid1,$etid2,$etid3,$etid4) = @id;
my ($etid11,$etid12,$etid13,$etid14) = @id1;
print "$arrSize,$arrSize1 \n" ;
print(" Option name is $etid1,$etid2,$etid3,$etid4 : $etid11,$etid12,$etid13,$etid14 ");
print Dumper $fd ;

Finally, All these has to be given to a sql query

Trying to get it
Any Help ,


Quote
perl dbaccess_test -e 10 11 12 13 -s 40 50 60 70 -d 04/25/2014 04/30/2014

Thanks
Tejas


(This post was edited by Tejas on May 30, 2014, 11:21 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives