hash troubles

hash troubles

am 15.04.2011 06:25:29 von dbro

Im trying to learn hashes but having a hard time trying to figure out
why it seems I cant use data from a hash outside of the original for
loop. This code works as expected to me. I guess it would be the
same if i just stuck a print statement after the hash assignment:


#!/usr/bin/perl -w
use strict;
our %hash;
our @array;
our $count;
our $value;
our $key;
print "Enter data then press ctrl D:\n";
@array = ;
chomp @array;
print "@array\n";
$count = 1;
foreach ( sort @array ) {
if (! $hash{$_} ) {
%hash = ( $_ => $count );
} else {
$hash{$_} = $hash{$_} + 1;
}
while (( $key, $value ) = each %hash) {
print "$key has been seen $value times\n";
}
}


But when I try to just print a total instead of every instance of the
words type at command prompt it seems to just hold onto just the last
value in hash. This what is the code:


#!/usr/bin/perl -w
use strict;
our %hash;
our @array;
our $count;
our $value;
our $key;
print "Enter data then press ctrl D:\n";
@array = ;
chomp @array;
print "@array\n";
$count = 1;
foreach ( sort @array ) {
if (! $hash{$_} ) {
%hash = ( $_ => $count );
} else {
$hash{$_} = $hash{$_} + 1;
}
}
while (( $key, $value ) = each %hash) {
print "$key has been seen $value times\n";
}


Any help would be greatly appreciated.
Thank You


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: hash troubles

am 16.04.2011 12:12:56 von Octavian Rasnita

From: "dbro"
> Im trying to learn hashes but having a hard time trying to figure out
> why it seems I cant use data from a hash outside of the original for
> loop. This code works as expected to me. I guess it would be the
> same if i just stuck a print statement after the hash assignment:
>=20
>=20
> #!/usr/bin/perl -w
> use strict;
> our %hash;
> our @array;
> our $count;
> our $value;
> our $key;
> print "Enter data then press ctrl D:\n";
> @array =3D ;
> chomp @array;
> print "@array\n";
> $count =3D 1;
> foreach ( sort @array ) {
> if (! $hash{$_} ) {
> %hash =3D ( $_ =3D> $count );
> } else {
> $hash{$_} =3D $hash{$_} + 1;
> }
> while (( $key, $value ) =3D each %hash) {
> print "$key has been seen $value times\n";
> }
> }
>=20
>=20


The line:

%hash =3D ( $_ =3D> $count );

re-creates a new hash each time it runs, and this program works only =
because you first sort the source array.

If the array wouldn't be sorted, and it would contain say the letters A =
B A B, it would print that A appears 1 time, B appears 1 time, A appears =
1 time...

If you want to print the elements of the array (with the number they =
appear) in order, you need to do the sort in the last loop:

#!/usr/bin/perl

use strict;
use warnings;

print "Enter data then press ctrl D (or CTRL+Z+ under =
Windows:\n";

my @array =3D ;
chomp @array;
print "@array\n";

my %hash;

for ( @array ) {
$hash{$_}++;
}

for my $key ( sort keys %hash ) {
print "$key has been seen $hash{$key} times\n";
}
Octavian


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: hash troubles

am 16.04.2011 14:58:54 von Shawn H Corey

On 11-04-15 12:25 AM, dbro wrote:
> if (! $hash{$_} ) {
> %hash = ( $_ => $count );
> } else {
> $hash{$_} = $hash{$_} + 1;

Use this instead:

$hash{$_} ++;


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/