Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

hash keys from another file

by Ninke (Novice)
on Mar 23, 2013 at 12:26 UTC ( #1025029=perlquestion: print w/replies, xml ) Need Help??
Ninke has asked for the wisdom of the Perl Monks concerning the following question:

Hello, once again seeking for your help. I have a file 'param' with '$count $word' on each line and a file 'sent' with some text, each word on each line
------param------- 200 dog 300 cat 100 rat 400 hen -----sent-------- The cat catches the hen ...
I need to tag each word in the 'sent' file with the corresponding count value from the 'param' file:
The cat 300 catches the hen 400 ...
And my code is:
#!/usr/bin/perl use strict; use utf8; use warnings; open PARAM, "param" or die $!; open DEV, "sent" or die $!; my %counts; my @dev; while(<DEV>){ chomp; @dev = <DEV>; } while(<PARAM>) { chomp; my($count, $word) = split / /; $counts{$word} = $count; } foreach my$word_dev(@dev){ if(exists $counts{$word_dev}){ print "$word_dev $counts{$word_dev}\n"; } }
I believe there is some silly mistake over there, but I can't find it. I will very appreciate your help:)

Replies are listed 'Best First'.
Re: hash keys from another file
by moritz (Cardinal) on Mar 23, 2013 at 12:33 UTC
    my @dev; while(<DEV>){ chomp; @dev = <DEV>; }

    This almost certainly does not do what you want.

    In the while(<DEV>) it reads a line from the file, in the chomp; line it removes the trailing newline, and then in the line @dev = <DEV> it ignores the read line, and reads all the rest of the lines into the array. Without any newline removal.

    What you probably meant to write was

    while (<DEV>) { chomp; push @dev, $_; }

    Or even simpler:

    my @dev = <DEV>; chomp @dev;
      Great, thank you and choroba for explaining me the mistake, now my code serves the purpose:)

        Hmm, Moritz explained you a specific mistake to be corrected. But Choroba told you something that you should not overlook and is at least as important in my view: load the param file into memory (a hash or whatever, depending on the specifics), but don't load the data file in memory if you can avoid it. It is usually far better to iterate over the data line by line when possible. This will save your day when your datafile becomes huge.

Re: hash keys from another file
by choroba (Chancellor) on Mar 23, 2013 at 12:31 UTC
    You do not need to load any file to an array. Just build a hash from the param file, then process the sent file line by line and output the count if you can find it:
    #!/usr/bin/perl use warnings; use strict; my %counts; open my $PARAM, '<', 'param' or die "param: $!"; while (<$PARAM>) { my ($count, $word) = split; $counts{$word} = $count; } open my $SENT, '<', 'sent' or die "sent: $!"; while (<$SENT>) { chomp; print; print " $counts{$_}" if exists $counts{$_}; print "\n"; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025029]
Approved by 2teez
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-06-25 11:19 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (567 votes). Check out past polls.