Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accepting array from command line

Status
Not open for further replies.

makarandmk

Programmer
Jan 25, 2003
10
US
I want to pass an array from CGI perl form and accept that array as a command line argument in other file. How do I do that?

My code below:
in CGI script I am calling some perl script with 4 parameters.
e.g. sample.pl var1 var2 var3 array

Now, in my sample.pl, how do I accept the 4th param?
I mean i will get,

$variable1=$ARGV[0];
$variable2 =$ARGV[1];
$variable3 =$ARGV[2];

???? =$ARGV[3]; <<I want to accept array here >>


 
You should be able to set it:

@testarray = $ARGV[3];
 
If you pass your array as individual arguments starting at the fourth, like: [tt]sample.pl var1 var2 var3 arrval1 arrval2[/tt]

then you could set the array in the script like:
[tt]@arrayarg = @ARGV[3..$#ARGV];[/tt]
 
Hi Friends,

That makes a good sence.. thanks very much for your replies.

regards,
Makarand
 
Sorry, I have some problem again .... Pl advise..!!

If you pass an array as individual arguments starting at the fourth, like:
sample.pl var1 var2 var3 arrval1 arrval2

then I could set the array in the script like:
@arrayarg = @ARGV[3..$#ARGV];

But if I want to pass 2 arrays, then what could I do?
I mean the following:

sample.pl var1 var2 var3 array1 array2

@arrayFirst = @ARGV[3..??];
@arraySecond = @ARGV[????];
 
I have written a short perl program to demonstrate:

#!/usr/bin/perl -w

my $one = $ARGV[0];
my $two = $ARGV[1];
my $three = $ARGV[2];
my @cmdarg1 = @ARGV[3..5];
my @cmdarg2 = @ARGV[6..8];

print &quot;one is: $one\n&quot;;
print &quot;two is: $two\n&quot;;
print &quot;three is: $three\n&quot;;
print &quot;\@cmdarg1 is: @cmdarg1\n&quot;;
print &quot;\@cmdarg2 is: @cmdarg1\n&quot;;
print &quot;cmdarg1[1] is: $cmdarg1[0]\n&quot;;
print &quot;cmdarg1[2] is: $cmdarg1[1]\n&quot;;
print &quot;cmdarg1[3] is: $cmdarg1[2]\n&quot;;
print &quot;cmdarg2[1] is: $cmdarg2[0]\n&quot;;
print &quot;cmdarg2[2] is: $cmdarg2[1]\n&quot;;
print &quot;cmdarg2[3] is: $cmdarg2[2]\n&quot;;

The output is as follows:

bjp01@localhost$ ./test.pl one two three arg1 arg2 arg3 arg4 arg5 arg6

one is: one
two is: two
three is: three
@cmdarg1 is: arg1 arg2 arg3
@cmdarg2 is: arg1 arg2 arg3
cmdarg1[1] is: arg1
cmdarg1[2] is: arg2
cmdarg1[3] is: arg3
cmdarg2[1] is: arg4
cmdarg2[2] is: arg5
cmdarg2[3] is: arg6

This will work if you knwo how many entries there will be into each array.
 
There will be no way to do what you ask if you don't know how many elements each array is going to have. You could pass those as arguments to the script.

Modifying bpinos' example, above:
[tt]my $one = $ARGV[0];
my $two = $ARGV[1];
my $three = $ARGV[2];
my $arr1len = $ARGV[3];
my $arr2len = $ARGV[4];
my @cmdarg1 = @ARGV[5..(5 + $arr1len - 1)];
my @cmdarg2 = @ARGV[(5 + $arr1len)..
(5 + $arr1len + $arr2len - 1)];
[/tt]
 
If you don't know how many elements will be in the array you are passing in, then you could use some sort of while loop and look through each element in the ARGV array and when you reach some separation field (i.e. something that denotes the next array) then you start loading values into that array.

It seems like it may be a bit much, and there may be an easier way that I haven't thought of yet.
 
Hi friends,

This is what exactly I did! Thanks very very much!!

regards,
Makarand
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top