Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

hash array

by borovez (Initiate)
on Mar 11, 2012 at 22:24 UTC ( [id://959040]=perlquestion: print w/replies, xml ) Need Help??

borovez has asked for the wisdom of the Perl Monks concerning the following question:

I am using a hash array to read in a file of words and definitions which are both seperated by a "/" which Is why I split the lines. I think I am storing the words and definitions correctly in the dictionary array but when I try to print the keys in the dictionary underneath (in sub readWords) it doesnt do anything??? Also how come it also doesnt work in my sub sortDictionary??? Please help and thanks.
my %dictionary; readWords(\%dictionary); sortDictionary(\%dictionary); sub readWords { $numWords = 0; open FILE, $ARGV[0] or die $!; while (my $lines = <FILE>) { $numWords++; chomp $lines; my ($words, $definitions) = split('/', $lines); $words = lc($words); $dictionary->{$words} = $definitions; } foreach $key (%dictionary) { print $dictionary->{$words}; print $result . "\n"; } close FILE; } sub sortDictionary { foreach my $key (sort keys %{dictionary}) { $key =~ /^(.)/; my $line = $key . '/' . $dictionary->{$key} . "\n"; print $line; } }

Replies are listed 'Best First'.
Re: hash array
by repellent (Priest) on Mar 11, 2012 at 22:59 UTC
    %dictionary is a hash. You are passing it as a hash reference argument to readWords() and sortDictionary(), via \%dictionary. So far so good.

    Now, inside the functions readWords() and sortDictionary(), you need to consume the argument you passed in. But your code currently does not do that. (Instead, it is consuming the closed-over lexical my %dictionary - if you don't understand this last sentence, ignore it for now).

    Let change your functions to consume the passed in hashref. To avoid confusion, I renamed the variable inside the functions to be $dict instead. Here's the same code with modifications indicated:
Re: hash array
by aaron_baugher (Curate) on Mar 12, 2012 at 00:51 UTC

    The actual problem is that you're alternating between a hash and hashref. You create a hash, %dictionary. But when you try to access it as $dictionary->{$key}, you're treating it as a hashref called $dictionary, which doesn't exist. I suspect that if you used strict and warnings, there would be complaints. Use $dictionary{$key} instead, treating it as a hash, and it'll work.

    There's also a stylistic problem, in that you're passing a hashref to your sub and not using it. That doesn't break your program, because defining the variable with my makes it local to the file, including the subs. So you can access %dictionary within your subs without passing it as an argument, but if you're going to do that, you should remove the argument from the subroutine call, because it makes it look like you forgot something.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

Re: hash array
by wamos (Initiate) on Mar 11, 2012 at 22:57 UTC
    Try this ...
    my %dictionary; readWords(\%dictionary); sortDictionary(\%dictionary); sub readWords { $numWords = 0; open FILE, $ARGV[0] or die $!; while (my $lines = <FILE>) { $numWords++; chomp $lines; my ($words, $definitions) = split('/', $lines); $words = lc($words); $dictionary{$words} = $definitions; } foreach $words (keys(%dictionary)) { print "$words = ",$dictionary{$words},"\n"; } close FILE; } sub sortDictionary { foreach my $key (sort keys %dictionary) { $key =~ /^(.)/; my $line = $key . '/' . $dictionary{$key} . "\n"; print $line; } }
    Hope that does what you want
    C:\> x.pl x.txt cat = nice animal snake = nasty animal dog = best friend cat/nice animal dog/best friend snake/nasty animal
Re: hash array
by wamos (Initiate) on Mar 11, 2012 at 22:59 UTC
    Try this ...
    my %dictionary; readWords(\%dictionary); sortDictionary(\%dictionary); sub readWords { $numWords = 0; open FILE, $ARGV[0] or die $!; while (my $lines = <FILE>) { $numWords++; chomp $lines; my ($words, $definitions) = split('/', $lines); $words = lc($words); $dictionary{$words} = $definitions; } foreach $words (keys(%dictionary)) { print "$words = ",$dictionary{$words},"\n"; } close FILE; } sub sortDictionary { foreach my $key (sort keys %dictionary) { $key =~ /^(.)/; my $line = $key . '/' . $dictionary{$key} . "\n"; print $line; } }
    Hope that is what you expected ...
    C:\> x.pl x.txt cat = nice animal snake = nasty animal dog = best friend cat/nice animal dog/best friend snake/nasty animal
      Hey... you guys are phenominal.... thank you very very much for your help. Both of your methods were great! This worked flawlessly

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://959040]
Approved by repellent
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found