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

RE: Hash Question

Thread Previous | Thread Next
From:
Balint, Jess
Date:
February 1, 2002 13:06
Subject:
RE: Hash Question
Message ID:
CA8ED43817EFD211855E00805FE6FD7403AA194B@scadmail2.alldata.net
The way I have the argument parsing set up is for ( 0..$#ARGV ) { . . .
When a -f n is presented on the command line, n will be the field number for
the first hash.
So if I use -f 1 -f 2 -f 3, there will be three levels of hashes. The top
level will contain all unique values in the first field of the file. The
second level will contain all unique values unique to the top level, and the
third level will be unique to the first and second level. The value of the
third level key will be a count of the occurance of all the values together
in the same line.

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

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