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

Re: More Economic Use of Hash

Thread Previous | Thread Next
From:
Roger C Haslock
Date:
February 1, 2002 01:57
Subject:
Re: More Economic Use of Hash
Message ID:
00a701c1ab07$26d37e80$245808c3@neliac
Do you want a lexical sort or a numeric sort?

foreach my $key ( sort keys %freq ) # lexical
foreach my $key ( sort ($a<=>$b) keys %freq ) # numeric

- Roger -

----- Original Message -----
From: "Balint, Jess" <JBalint@alldata.net>
To: "'Roger C Haslock'" <roger@haslock.com>; <beginners@perl.org>
Sent: Thursday, January 31, 2002 9:37 PM
Subject: RE: More Economic Use of Hash


> Thanks. Works great. Any ideas how I might sort the output hash for output
> to a file in key order?
>
> -----Original Message-----
> From: Roger C Haslock [mailto:roger@haslock.com]
> Sent: Thursday, January 31, 2002 2:23 PM
> To: Balint, Jess; beginners@perl.org
> Subject: Re: More Economic Use of Hash
>
>
> # Why not ...
> while( <INFILE> )
> {
>     @tmp = split( /\|/ );
>     if (exists $freq{$tmp[$table]})
>     {
>         $freq{$tmp[$table]}++
>     }
>     else
>     {
>         $freq{$tmp[$table]}=1
>     }
>     $tot ++
> }
>
> ----- Original Message -----
> From: "Balint, Jess" <JBalint@alldata.net>
> To: <beginners@perl.org>
> Sent: Thursday, January 31, 2002 6:45 PM
> Subject: More Economic Use of Hash
>
>
> > I am attemping to create a frequency chart based on a pipe delimited
> > database output. Following is what I have come up with. The hash create
> from
> > this is then output to another file. It seems to be very slow on files
in
> > excess of ~10000 lines. This will be used on ~500000 line files and
needs
> to
> > be a proficient as possible. If anyone knows a better way to do this, it
> > would be extremely helpful. Thank you. -Jess
> >
> > $table is the number of the field which the frequency will be calculated
> on.
> > ex. a|b|c|d| if $table = 3 then c will be used
> > @tmp is the split array of the line input
> > %freqidx is the hash with fields values as keys and frequency counts
> > $tot is for percentage calculation after this loop. this is basically an
> > INFILE line count
> >
> > while( <INFILE> ) {
> >         @tmp = split( /\|/ );
> >         $x = 0;
> >         foreach( keys( %freqidx ) ) {
> >                 if( $tmp[$table] ne $_ ) {
> >                         $x = 1;
> >                 } else {
> >                         $x = 0;
> >                         last;
> >                 }
> >         }
> >         if( $x == 1 ) {
> >                 $freqidx{$tmp[$table]} = 0;
> >         }
> >         $freqidx{$tmp[$table]}++;
> >         $tot++;
> > }
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> >
> >
> >
> >
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>
>
>
>


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