Re: getting arguments



frytaz@xxxxxxxxx wrote:

This script works, but when I'm trying to use it with some other array

use strict;
use warnings;

missing.

sub myagr {
my ($data, $server, $witem) = @_;
my ($cmd, @rest) = split(/ /, $data);

$opts = '^-(one|two|three|four|five)$';

$rest[0] =~ /$opts/ or $badarg=1;

if ($badarg eq 1) {
print "Bad arguments";
} else {
$state;
%args;
for ( @rest ) {
if ( /$opts/ ) {
$state = $1;
$args{ $state } = [ ];
next;
}
push @{ $args{ $state } }, $_;
}
my ( $one, $two, $three, $four, $five ) = map { join ' ',
@{ $args{ $_ } } } qw/ one two three four five /;
# ^ in that line $_ doesn't contain any data

print "one $one two $two three $three four $four five $five";
#prints one two three four five
}
}

why $_ losing data?

I don't understand what you mean. Assuming that this line is added to your script:

myagr ( "$0 @ARGV" );

this is the result I get:

C:\home>perl test.pl "-one first_arg -two second_arg -three third_arg -four fourth_arg -five fifth_arg"
one first_arg two second_arg three third_arg four fourth_arg five fifth_arg
C:\home>

Another thing is that it is a very odd way of passing arguments to a script, and has little to do with what Jens showed you...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
.



Relevant Pages

  • Shell functions to manipulate the PATH
    ... Yes, quite often when the newbies want to change the PATH it is misguided, ... because they are trying to run a /sbin command as ordinary users. ... Then the script is less ... # `path --append args' appends args, ...
    (comp.os.linux.setup)
  • Re: [KSH 88] How to use set -A when first value is -x
    ... : optional args. ... It uses getopts to parse the options (I guess that was ... obvious) and a for x in $@ to parse out the mandatory arguments. ... of the script. ...
    (comp.unix.shell)
  • Re: File dialogs
    ... However, it seems that each time I use tk_getOpenFile, it starts off from the current script folder instead of where the user made the last selection. ... proc mem_tk_getOpenFile {args} { ... # in case of unbalanced args, ... # and to be compliant with the wrapped command ...
    (comp.lang.tcl)
  • Re: Finding name of a file in a directory
    ... You just ensure that it accepts command line args. ... to the script, but I usually just give them an hta and have them browse ... without even the implied warranty of merchantability ...
    (microsoft.public.scripting.vbscript)
  • Re: Zip and Unzip files and folders
    ... >> I have a script that makes a backup of my info, in win2003 server, but I ... > Dim Args, Arg, SA, Dst, Zip, ZipFolder, Src ... > Set ZipFolder = SA.NameSpace ... > Sub ShowUsage ...
    (microsoft.public.scripting.vbscript)