|
Posted by Ela on March 20, 2008, 8:14 pm
Please log in for more thread options
I am declaring the following variables at the beginning:
@usage='cg.pl afile gfile ffile outname gensf genqcf genpf';
die "Usage: @usage\n" if $#ARGV < @usage;
my ($afile, $gfile, $ffile, $outname, $gensf, $genqcf, $genpf);
I have 2 more questions here, the first, instead of copying the @usage to
the my(), how can I better write the codes to avoid typo and work? I will
use exactly the same name indeed and don't know how many more I will add. I
don't like config file because it usually makes program no longer work after
a period of maintenance (config file gets lost etc).
The second question is, how to allow the user input the arguments without
restricting them in a specific order? e.g. gfile ahead of afile...?
I find that some of the arguments are necessary, while some others, such as
flags, may be optional. How can I write it in a better way to check whether
all the necessary arguments are provided instead of fixing $#ARGV < @usage?
|
|
Posted by sandy_saydakov on March 20, 2008, 8:35 pm
Please log in for more thread options
> I am declaring the following variables at the beginning:
>
> @usage='cg.pl afile gfile ffile outname gensf genqcf genpf';
> die "Usage: @usage\n" if $#ARGV < @usage;
>
> my ($afile, $gfile, $ffile, $outname, $gensf, $genqcf, $genpf);
>
> I have 2 more questions here, the first, instead of copying the @usage to
> the my(), how can I better write the codes to avoid typo and work? I will
> use exactly the same name indeed and don't know how many more I will add. I
> don't like config file because it usually makes program no longer work after
> a period of maintenance (config file gets lost etc).
> The second question is, how to allow the user input the arguments without
> restricting them in a specific order? e.g. gfile ahead of afile...?
> I find that some of the arguments are necessary, while some others, such as
> flags, may be optional. How can I write it in a better way to check whether
> all the necessary arguments are provided instead of fixing $#ARGV < @usage?
I would suggest using Getopt::Long http://perldoc.perl.org/Getopt/Long.html
/sandy
http://myperlquiz.com/
|
|
Posted by benkasminbullock@gmail.com on March 20, 2008, 8:48 pm
Please log in for more thread options > I am declaring the following variables at the beginning:
>
> @usage='cg.pl afile gfile ffile outname gensf genqcf genpf';
> die "Usage: @usage\n" if $#ARGV < @usage;
>
> my ($afile, $gfile, $ffile, $outname, $gensf, $genqcf, $genpf);
>
> I have 2 more questions here, the first, instead of copying the @usage to
> the my(), how can I better write the codes to avoid typo and work?
What I would do is to use a hash (here called %args) to store the
variables:
#! perl
use warnings;
use strict;
my @usage=qw/afile gfile ffile outname gensf genqcf genpf/;
die "Usage: cg.pl @usage\n" if @ARGV < @usage;
my %args;
@args = @ARGV;
print join (" ", %args),"\n";
$ ./usage.pl a b c d e f
Usage: cg.pl afile gfile ffile outname gensf genqcf genpf
$ ./usage.pl a b c d e f g
genpf g genqcf f gensf e gfile b outname d ffile c afile a
> I will
> use exactly the same name indeed and don't know how many more I will add. I
> don't like config file because it usually makes program no longer work after
> a period of maintenance (config file gets lost etc).
> The second question is, how to allow the user input the arguments without
> restricting them in a specific order? e.g. gfile ahead of afile...?
It's not possible for me to answer that question without information
about how the program knows how to distinguish between gfile, afile,
etc.
> I find that some of the arguments are necessary, while some others, such as
> flags, may be optional. How can I write it in a better way to check whether
> all the necessary arguments are provided instead of fixing $#ARGV < @usage?
I'll leave that as an exercise for you to attempt yourself.
|
|
Posted by ccc31807 on March 21, 2008, 7:40 am
Please log in for more thread options In your script below, I have not seen the construction used on line 7.
On Mar 20, 7:48 pm, "benkasminbull...@gmail.com"
> #! perl
> use warnings;
> use strict;
> my @usage=qw/afile gfile ffile outname gensf genqcf genpf/;
> die "Usage: cg.pl @usage\n" if @ARGV < @usage;
> my %args;
> @args = @ARGV;
> print join (" ", %args),"\n";
@ARVG is the array passed in from the CL.
@usage is the array created on line 4.
%args is the hash you use to link @ARGV and @usage.
I understand how you can iterate through the hash like this, where
each hash element has the (scalar) name $args:
foreach my $key (keys %args){print "\t$key => $args\n";}
What I DON'T understand is the use of the @ symbol to declare array
elements, as in @args. I've searched for documentation of this
usage but have not found it. I can see how you would pass @ARGV to
@usage to copy one array to another, as in @second = @first.
I can't see how you can represent %args as @args, even though
accessing individual elements by $args{} is clear. I would appreciate
it greatly if you could explain the workings to me, or refer me to
some documentation of this feature.
Thanks, CC.
|
|
Posted by Lawrence Statton on March 21, 2008, 10:41 am
Please log in for more thread options
> In your script below, I have not seen the construction used on line 7.
>
> On Mar 20, 7:48 pm, "benkasminbull...@gmail.com"
> > #! perl
> > use warnings;
> > use strict;
> > my @usage=qw/afile gfile ffile outname gensf genqcf genpf/;
> > die "Usage: cg.pl @usage\n" if @ARGV < @usage;
> > my %args;
> > @args = @ARGV;
> > print join (" ", %args),"\n";
>
> @ARVG is the array passed in from the CL.
> @usage is the array created on line 4.
> %args is the hash you use to link @ARGV and @usage.
>
> [deletia...]
>
> What I DON'T understand is the use of the @ symbol to declare array
hash
> elements, as in @args. I've searched for documentation of this
> usage but have not found it. I can see how you would pass @ARGV to
> @usage to copy one array to another, as in @second = @first.
It's a hash slice
Some code to elucidate slices:
In an ARRAY (because they're slightly more intuitive ...)
my @person;
$person[0] = 'Fred';
$person[3] = 'Mary';
$person[5] = 'Georgia';
OR you can combine that into
@person[0,3,5] = qw / Fred Mary Georgia /;
Now, we can do the same thing with a HASH
my %hash;
$hash = 'Red';
$hash = 'Yellow';
$hash = 'Green';
OR
my %hash;
@hash{qw/ apple banana kiwi /} = qw/ Red Yellow Green /;
> I can't see how you can represent %args as @args, even though
> accessing individual elements by $args{} is clear. I would appreciate
> it greatly if you could explain the workings to me, or refer me to
> some documentation of this feature.
`perldoc perldata` offers....
If you're confused about why you use an '@' there on a hash slice
instead of a '%', think of it like this. The type of bracket (square or
curly) governs whether it's an array or a hash being looked at. On the
other hand, the leading symbol ('$' or '@') on the array or hash
indicates whether you are getting back a singular value (a scalar) or a
plural one (a list).
--
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
place them into the correct order.
|
| Similar Threads | Posted | | NetServer::Generic -- welcome message ?? | March 30, 2006, 5:04 am |
| generic increment of number/string | November 21, 2006, 11:21 am |
| Generic Syntax highlighting module | June 17, 2007, 9:33 am |
| [perl-python] generic equivalence partition | February 24, 2005, 4:48 am |
| Creating and outputting a generic data structure? | January 17, 2006, 10:00 am |
| Generic fingerprint ie hash value for compex data structure? | July 24, 2004, 9:39 am |
| Detecting the declaration of a variable? | October 21, 2005, 11:18 am |
| Strange asterisk use in a hash declaration | November 23, 2004, 6:31 am |
| been using Java too long: please help with dynamic "our" declaration | January 25, 2006, 2:52 pm |
| Variable declaration - C vs script style | July 31, 2007, 8:01 am |
|