getting arguments

F

frytaz

Hi there
I want to run my per script with arguments, for instance:
../script.pl -one value one text 1 -two value two text 2 -three value
three text 3
and i want to get those arguments in
$one = value one text 1
$two = value two text 2
$three = value three text 3

i tried with regular expression m/-one (.+?) -two (.+?) -three (.+?)/
and it works but when i mix-up arguments like:
../script.pl -two value two text 2 -three value three text 3 -one value
one text 1
then i need to use different regex.

Someone know other solution ?

Thanks in advance
 
K

kens

Hi there
I want to run my per script with arguments, for instance:
./script.pl -one value one text 1 -two value two text 2 -three value
three text 3
and i want to get those arguments in
$one = value one text 1
$two = value two text 2
$three = value three text 3

i tried with regular expression m/-one (.+?) -two (.+?) -three (.+?)/
and it works but when i mix-up arguments like:
./script.pl -two value two text 2 -three value three text 3 -one value
one text 1
then i need to use different regex.

Someone know other solution ?

Thanks in advance

Take a look at the Getopt::Long module.

perldoc Getopt::Long

Note that if there are spaces in the option strings, they will have to
be quoted on the command line.

For example, ./script.pl -two "value two text 2" -three "value three
text"

HTH, Ken
 
K

kens

Take a look at the Getopt::Long module.

perldoc Getopt::Long

Note that if there are spaces in the option strings, they will have to
be quoted on the command line.

For example, ./script.pl -two "value two text 2" -three "value three
text"

HTH, Ken

Of course that example should have been :

../script.pl --two "value two text 2" --three "value three text"

Have to use double dashs before the option if it is not a single
character.

Ken
 
J

Jens Thoms Toerring

I want to run my per script with arguments, for instance:
./script.pl -one value one text 1 -two value two text 2 -three value
three text 3

The usual convention with atrguments is to have them enclosed in
(double) quotes when they contain spaces, so they arrive as a
whole in your program
and i want to get those arguments in
$one = value one text 1
$two = value two text 2
$three = value three text 3
i tried with regular expression m/-one (.+?) -two (.+?) -three (.+?)/
and it works but when i mix-up arguments like:
./script.pl -two value two text 2 -three value three text 3 -one value
one text 1
then i need to use different regex.

Perhaps this does the trick:

#!/usr/bin/perl

use strict;
use warnings;

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

$ARGV[ 0 ] =~ /$opts/ or die "Invalid arguments\n";

my $state;
my %args;
for ( @ARGV ) {
if ( /$opts/ ) {
$state = $1;
$args{ $state } = [ ];
next;
}
push @{ $args{ $state } }, $_;
}

my ( $one, $two, $three ) = map { join ' ', @{ $args{ $_ } } }
qw/ one two three /;

Regards, Jens
 
F

frytaz

I want to run my per script with arguments, for instance:
./script.pl -one value one text 1 -two value two text 2 -three value
three text 3

The usual convention with atrguments is to have them enclosed in
(double) quotes when they contain spaces, so they arrive as a
whole in your program
and i want to get those arguments in
$one = value one text 1
$two = value two text 2
$three = value three text 3
i tried with regular expression m/-one (.+?) -two (.+?) -three (.+?)/
and it works but when i mix-up arguments like:
./script.pl -two value two text 2 -three value three text 3 -one value
one text 1
then i need to use different regex.

Perhaps this does the trick:

#!/usr/bin/perl

use strict;
use warnings;

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

$ARGV[ 0 ] =~ /$opts/ or die "Invalid arguments\n";

my $state;
my %args;
for ( @ARGV ) {
if ( /$opts/ ) {
$state = $1;
$args{ $state } = [ ];
next;
}
push @{ $args{ $state } }, $_;

}

my ( $one, $two, $three ) = map { join ' ', @{ $args{ $_ } } }
qw/ one two three /;

Regards, Jens

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

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?
 
U

Uri Guttman

fc> why $_ losing data?

because you are not using one of the core modules to get command line
arguments. use them and don't write a broken one yourself.

uri
 
G

Gunnar Hjalmarsson

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...
 
G

Gunnar Hjalmarsson

Jens said:
my $state;
my %args;
for ( @ARGV ) {
if ( /$opts/ ) {
$state = $1;
$args{ $state } = [ ];
next;
}
push @{ $args{ $state } }, $_;
}

my ( $one, $two, $three ) = map { join ' ', @{ $args{ $_ } } }
qw/ one two three /;

Why the deep data structure? Why not just:

my ($state, %args);
for ( @ARGV ) {
if ( /$opts/ ) {
$state = $1;
next;
}
$args{ $state } = $_;
}

my ( $one, $two, $three ) = @args{ qw/ one two three / };
 
J

Jens Thoms Toerring

This script works, but when I'm trying to use it with some other array
sub myagr {
my ($data, $server, $witem) = @_;
my ($cmd, @rest) = split(/ /, $data);

It's impossible to say what's happening here unless you tell
exactly with what arguments the function is called. But it
already looks suspicious that $data is a scalar. It only
could work if you call the function with something like

myagr( ( join ' ', ( $0, @ARGV ) ), ...)

but then I wouldn't know why you first join the scripts name and
the command line arguments into a single string to pass it to
the function, only to split it again within the function...
$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?

My best guess is @rest doesn't contain what you expect it to,
did you try to print it out to see what elements it actually
has?
Regards, Jens
 
J

Jens Thoms Toerring

Gunnar Hjalmarsson said:
Jens said:
my $state;
my %args;
for ( @ARGV ) {
if ( /$opts/ ) {
$state = $1;
$args{ $state } = [ ];
next;
}
push @{ $args{ $state } }, $_;
}

my ( $one, $two, $three ) = map { join ' ', @{ $args{ $_ } } }
qw/ one two three /;
Why the deep data structure? Why not just:
my ($state, %args);
for ( @ARGV ) {
if ( /$opts/ ) {
$state = $1;
next;
}
$args{ $state } = $_;

But then you would only store the last of the words, i.e. for the
command line argument "-one one test 1", would would only keep
the "1" and would not get "one test 1" as I understand the OP
wanted. Or am I missing something?

Regards, Jens
 
G

Gunnar Hjalmarsson

Jens said:
But then you would only store the last of the words, i.e. for the
command line argument "-one one test 1", would would only keep
the "1" and would not get "one test 1" as I understand the OP
wanted. Or am I missing something?

I understand now that your way would permit to _not_ group words
together with e.g. quotes. OTOH you also say:

"The usual convention with atrguments is to have them enclosed in
(double) quotes when they contain spaces, so they arrive as a
whole in your program"

I think I tend to believe that a recommendation to use quotes for
grouping is preferrable.
 
J

Jens Thoms Toerring

I understand now that your way would permit to _not_ group words
together with e.g. quotes. OTOH you also say:
"The usual convention with atrguments is to have them enclosed in
(double) quotes when they contain spaces, so they arrive as a
whole in your program"
I think I tend to believe that a recommendation to use quotes for
grouping is preferrable.

Definitely, but the OP may have his reasons to do it otherwise
and who am I to tell him what to do;-)

Regards, Jens
 
M

Michele Dondi

Yep. :)

I'm glad someone got it - I dread the day when a Monty Python reference is
too obscure for this group...

Well, I was about to make a joke about a very well known programming
language occasionally compared with Perl... but I'm refraining to...


Michele
 
T

Ted Zlatanov

LS> Something like ...

LS> The American comedy troupe Monty Perl was far superior to those lame
LS> brits....

LS> "This is an undef parrot, he has been garbage collected ..... "

(Waitress) "We have the $ and %browns, $ and @eggs, $! and $@, $! $! $! $@ $@ and $?, $_ $_ $?
$! $! $_ cheese and $."

[NB for those who don't have it in their country: "hash browns" are a type of food]

(Hag) "Do you have anything without $"

(W) "Well, the @$ref doesn't have much $ in it..."

(Vikings) "$! $_ $! $_ $? $@ $_ \$_ \$_ $!"

(H) "I don't like $ and I miss Java variables!!!"



Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,218
Latest member
NatishaFin

Latest Threads

Top