develooper Front page | perl.beginners | Postings from February 2002

RE: Hash Question

Thread Previous | Thread Next
From:
Chas Owens
Date:
February 4, 2002 12:18
Subject:
RE: Hash Question
Message ID:
1012853914.14800.1418.camel@tert.icallinc.com
Not that I am aware of, but if you are not tied to a given command line
structure then you could do this:

<example name="t2.pl">
#!/usr/bin/perl

use strict;
use Getopt::Std;

my %args;

getopt("f", \%args);

my @keys = split / /, $args{'f'};

print "key $_ = field $keys[$_]\n" for (0..$#keys);
</example>

<output from="t2.pl -f '1 2 3 10'">
key 0 = field 1
key 1 = field 2
key 2 = field 3
key 3 = field 10
</output>

On Mon, 2002-02-04 at 14:53, Balint, Jess wrote:
> Thanks, the first one works great. Now after all that trouble is there any
> way to use Getopt::Std to parse these? I found it in the Perl Cookbook.
> Here's what it says:
> 
> use Getopt::Std;
> 
> # -v ARG, -D ARG, -o ARG, sets $opt_v, $opt_D, $opt_o
> getopt("vDo");              
> # -v ARG, -D ARG, -o ARG, sets $args{v}, $args{D}, $args{o}
> getopt("vDo", \%args);
> 
> getopts("vDo:");         # -v, -D, -o ARG, sets $opt_v, $opt_D, $opt_o
> getopts("vDo:", \%args); # -v, -D, -o ARG, sets $args{v}, $args{D}, $args{o}
> 
> Now, that is fine but can it get multiple values for a single argument, '-f'
> here and store an array reference in the hash for all the values of '-f'
> arguments? If so, it may be easier. Thanks for all your help.
> 
> --Jess
> 
> -----Original Message-----
> From: Chas Owens [mailto:cowens@intercall.com]
> Sent: Monday, February 04, 2002 2:56 PM
> To: Balint, Jess
> Subject: RE: Hash Question
> 
> 
> You know, I almost always test my examples, and when I don't it always
> bites me.  The following examples have been tested this time.  Try one
> of these instead:
> 
> <example type="short">
> for (0..$#ARGV) {
>         if ($ARGV[$_] =~ /^-f(.+)|^-f/) {
>                 if (defined($1)) {
>                         push @keys, $1;
>                 } else {
>                         $_++;
>                         push @keys, $ARGV[$_];
>                 }
> 	}
> }
> </example>
> 
> <example type="perlish">
> while (@ARGV) {
>         $_ = shift @ARGV;
>         if (/^-f(.+)|^-f/) {
>                 if (defined($1)) {
>                         push @keys, $1;
>                 } else {
>                         push @keys, shift @ARGV;
>                 }
>         }
> }
> </example>
> 
> <example type="safer perlish">
> my @argv = @ARGV;
> while (@argv) {
>         $_ = shift @argv;
>         if (/^-f(.+)|^-f/) {
>                 if (defined($1)) {
>                         push @keys, $1;
>                 } else {
>                         push @keys, shift @argv;
>                 }
>         }
> }
> </example>
> 
> On Mon, 2002-02-04 at 13:53, Balint, Jess wrote:
> > That seems like the best way to do it, but if I enter -f 3, $tables[n] = "
> "
> > and not 3 like it should. I think that $1 is defined as " " in this
> > argument. What can I do about this?
> > 
> > -----Original Message-----
> > 
> > On Fri, 2002-02-01 at 15:23, Balint, Jess wrote:
> > > A scalar value based on the number of command line arguments put into an
> > > array.
> > > 
> > >         if( $ARGV[$_] =~ /^-f/ ) {
> > >         # PARSE TABULATION VALUES
> > >                 if( $table ) {
> > >                         $table =  $ARGV[$_];
> > >                         $table =~ s/-f//;
> > >                         $table =  $ARGV[$_+1] if( length( $table ) == 0
> );
> > >                         $tables[$tblcnt] = $table;
> > >                         $tblcnt++;
> > >                 } else {
> > >                         $table =  $ARGV[$_];
> > >                         $table =~ s/-f//;
> > >                         $table =  $ARGV[$_+1] if( length( $table ) == 0
> );
> > >                         $tables[0] = $table;
> > >                         $tblcnt++;
> > >                 }
> > > 
> > <snip />
> > 
> > First off, you don't need $tblcnt.  @tables in a scalar context will
> > return the number of elements and you can simply push the value onto the
> > array (see perldoc -f push).  This also gets rid of the if $table
> > business.
> > 
> > Second off, I assume that you are trying to treat -f table and -ftable
> > the same.  In which case shouldn't you increment $_ if you grab the next
> > arg?  
> > 
> > if ( $ARGV[$_] =~ /^-f(.*)/ ) {
> > # PARSE TABULATION VALUES
> > 	if (defined($1)) {             #if there was something after -f
> > 		push @tables, $1;
> > 	} else {                       #otherwise use next arg
> > 		$_++;
> > 		push @tables, $ARGV[$_];
> > 	}
> > }
> > 
> > print "There were ", scalar(@tables), "tables on the cmdline.\n";
> > 
> > 
> > Thirdly, where are the keys for the hashes going to come from?  And how
> > are you going to know at which level in the hash you want to store the
> > data?
> > 
> > To clarify:
> > In my example I read the keys from the first three words of a line where
> > the first word was the first key, the second word was the second key,
> > and the third word was the the third key and then treated the fourth
> > word as the data.
> > 
> > -- 
> > Today is Boomtime the 32nd day of Chaos in the YOLD 3168
> > Hail Eris!
> > 
> > Missle Address: 33:48:3.521N  84:23:34.786W
> -- 
> Today is Setting Orange the 35th day of Chaos in the YOLD 3168
> Hail Eris, Hack Linux!
> 
> Missle Address: 33:48:3.521N  84:23:34.786W
-- 
Today is Setting Orange the 35th day of Chaos in the YOLD 3168


Missle Address: 33:48:3.521N  84:23:34.786W


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About