hash and counter - newbie 
Author Message
 hash and counter - newbie

Hello.

    I've got data like this:

B05
A74
B95
E42
A76
A98
A39
A39
D67
D67
D67
A39
A74
B95
A76
A90
E42
E42
E42

My point is sort this data and print number of times each entry appeared.
Till now I've got this code snipped:

use strict;

my %hasz;
my $value;

while (<>) {
  if (m#(.\d\d)#) {
    chomp;
    $hasz{$1}++;
#    print "$1\n";
   }

Quote:
}

foreach $key (sort keys %hasz)
{

 print "$key\n";

   }

but, how to add info about number of entries I mean i.e.

A74 = 2
D67 = 3

and so on. Any ideas ? Regards.

    Grzegorz Goryszewski



Fri, 17 Dec 2004 08:52:15 GMT  
 hash and counter - newbie
Also sprach Grzegorz Goryszewski:

Quote:
>     I've got data like this:

> B05
> A74
> B95

[...]

Quote:
> My point is sort this data and print number of times each entry appeared.
> Till now I've got this code snipped:

> use strict;

> my %hasz;
> my $value;

> while (<>) {
>   if (m#(.\d\d)#) {
>     chomp;
>     $hasz{$1}++;
> #    print "$1\n";
>    }

> }

> foreach $key (sort keys %hasz)
> {

>  print "$key\n";

>    }

> but, how to add info about number of entries I mean i.e.

> A74 = 2
> D67 = 3

You already accomplished the counting. Now all you have to do is print
it: :-)

    for my $key (sort keys %hasz) {
        print "$key = $hasz{$key}\n";
    }

Tassilo
--

pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;



Fri, 17 Dec 2004 09:38:45 GMT  
 hash and counter - newbie
on Mon, 01 Jul 2002 07:52:15 GMT, "Grzegorz Goryszewski"

Quote:

> use strict;

> my %hasz;
> my $value;

You are declaring '$value' but never use it.

Quote:

> while (<>) {
>   if (m#(.\d\d)#) {
>     chomp;
>     $hasz{$1}++;
> #    print "$1\n";
>    }

> }

> foreach $key (sort keys %hasz)

You have not declared $key - your code won't run under strict. Did
you cut-and-paste?

Quote:
> {

>  print "$key\n";

   print "$key = $hasz{$key}\n";

--
felix



Fri, 17 Dec 2004 09:40:33 GMT  
 hash and counter - newbie


Quote:
> on Mon, 01 Jul 2002 07:52:15 GMT, "Grzegorz Goryszewski"

> You have not declared $key - your code won't run under strict. Did
> you cut-and-paste?

Yes, but made some changes then, sorry and thanks for solution. I was so
close :). Regards.

    Grzegorz Goryszewski



Fri, 17 Dec 2004 09:47:25 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Newbie, split function, and counter

2. newbie....and counters..

3. Newbie: counter sometimes resets

4. Newbie: graphic counter:shes-a no work!

5. *perl newbie* asking for hash-of-hashes basics

6. newbie hashes of hashes declartion question

7. Hashes of Hashes of Hashes of Hashes....

8. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

9. Newbie question about hashes (and/or arrays)

10. Newbie: 'require' problem with hash params

11. newbie hash question

12. question: perl syntax, lists, hashes, contexts, and one confused newbie

 

 
Powered by phpBB® Forum Software