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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parameter arguments

Status
Not open for further replies.

pincode11

Programmer
Mar 27, 2006
5
SK
Hi all.
How can I handle more arguments by one parameter?
something like:

./myprogram.pl -arg1 par1 par2 par3 -arg2 -arg3

When I used Getoptions or getopt they handle only one argument by one parameter.

Thanks
 
You could:[tt]./myprogram.pl -arg1 par1|par2|par3 -arg2 -arg3[/tt], where the pipe symbol (|) is just any delimeter for your parameters, then just parse out the parameter list with a simple split.

- George
 
Yes, but I would like to know if there is some way to do this without some special delimiter...
 
@argv contains Number
$argv[0] ... $argv[n]

contain the arguments
 
You would have to write your own parameter processor.
It would simply have to walk through each parameter (from @ARGV) in turn and if it was "-arg1" for example, it would collect the next 3 values before continuing to look for more switches.


Trojan.
 
Getopt::Long can cope with multiples, but you have to code them as (e.g.)
Code:
myprog -a lib1 -a lib2 -a lib3
. It puts them into an array for you. See the doc, under 'options with multiple values'.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
an EXAMPLE of a arguments processor
Code:
my $current_switch = '--';

for (my $i=0 ; $i <= $#ARGV ; $i++)
{
    if ($ARGV[$i] =~ /^\-/)
    {
	$current_switch = $ARGV[$i];
	$my_argv{$current_switch} = ();
    }
    else
    {
	push @{$my_argv{$current_switch}},$ARGV[$i];
    }
}

foreach (keys %my_argv)
{
    print "$_  =   ",join(',',@{$my_argv{$_}}),"\n";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top