FAQ
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 = <STDIN>;
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 = <STDIN>;
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

Search Discussions

  • Octavian Rasnita at Apr 16, 2011 at 10:14 am
    From: "dbro" <devintb@gmail.com>
    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 = <STDIN>;
    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";
    }
    }

    The line:

    %hash = ( $_ => $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+<ENTER> under Windows:\n";

    my @array = <STDIN>;
    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
  • Shawn H Corey at Apr 16, 2011 at 12:59 pm

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 15, '11 at 4:25a
activeApr 16, '11 at 12:59p
posts3
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase