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

RE: Hash Question

Thread Previous | Thread Next
Balint, Jess
February 4, 2002 11:01
RE: Hash Question
Message ID:
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/ ) {
>                 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

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

if ( $ARGV[$_] =~ /^-f(.*)/ ) {
	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

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

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About