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

Hello
I've got two text files

c.txt:

Here I am this is me Theres nowhere else on earth Id rather be
d.txt:
Here noun am verb Theres advb
I'm storing the contents of d.txt in a hash, where the words are the keys and the grammatical categories are the values.

The contents of c.txt are stored in a hash slice, where the words are the keys.

I need to find words common to both c.txt and d.txt, that is common keys in both the hashes,and for each match write the gramm. category in c.txt next to the matched word.

I've tried some code,I think the prob. is at the end i.e. the file write to c.txt.The contents of c.txt are deleted and all I get is an empty file.

Any help would be appreciated.Thanx in advance.

open DF, 'd.txt' or die $!; my %tagdict; while (<DF>) { chomp; my ($word, $cat) = split; $tagdict{$word} = $cat; } while ( ($k,$v) = each %tagdict ) { #print "$k => $v\n"; } close DF; open CF, 'c.txt' or die $!; my %tagcorpus; while (<CF>) { chomp; my @cwords = split; @tagcorpus{@cwords} = (1) x @cwords; } while ( ($k,$v) = each %tagcorpus ) { #print "$k => $v\n"; } close CF; open CF, '>c.txt' or die $!; foreach (keys %tagdict){ print values %tagdict if exists $tagcorpus{$_}; }

update (broquaint): put text file examples in <code> tags and cleaned up formatting

Replies are listed 'Best First'.
Re: File write
by BrowserUk (Patriarch) on Jun 26, 2003 at 11:58 UTC

    The problem is that you reopen c.txt for output '>c.txt', which overwrites the file, and then print to the terminal.

    If you wish to keep the original contents of c.txt and append to it, reopen the file for append '>>c.txt'.

    And either way, you need to say where you want your output to go by adding the file handle to the print line

    foreach (keys %tagdict){ print CF values %tagdict if exists $tagcorpus{$_}; } # ^^ Here!

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      Hello!
      Yes, the missing file handle was a silly mistake.
      However after the file write I need c.txt to look like this, gramm. category against matched word:
      Here noun I am verb this is me Theres advb nowhere else on earth Id ra +ther be

      which the this code does not do:
      foreach (keys %tagdict){ print CF $tagdict{$_} if exists $tagcorpus{$_}; }

      I've tried to do this in another round-about way,but there's
      a problem with this is too.Here's the code:
      open DF, 'd.txt' or die $!; my %tagdict; while (<DF>) { chomp; my ($word, $cat) = split; $tagdict{$word} = $cat; } while ( ($k,$v) = each %tagdict ) { #print "$k => $v\n"; } close DF; open CF, '+<c.txt' or die $!; my %tagcorpus; while (<CF>) { chomp; my @cwords = split; @tagcorpus{@cwords} = (1) x @cwords; } while ( ($k,$v) = each %tagcorpus ) { #print "$k => $v\n"; } close CF; open CF, '+<c.txt' or die $!; seek CF, 0, 0; # go to start of file truncate CF, 0; foreach $ditem(keys %tagdict){ #print "\n$ditem"; foreach $vitem(values %tagdict){ #print "\n$vitem"; $_ = process($_); #print $_; print CF; print CF " "; foreach $citem(keys %tagcorpus) { #print "\n$vitem"; my $currentposition=tell CF; seek CF,0,1 ; if ($citem eq $ditem){ syswrite CF,"$vitem",4 ; } } } } close CF; sub process{ foreach $key(keys %tagcorpus){ #print "\n$key"; return($key); } } 1;

      The problem is in this part:
      foreach $key(keys %tagcorpus){ #print "\n$key"; return($key); }

      I can't figure out why the print statement keeps printing
      the same word as a key value.Towards the top of the program in
      while ( ($k,$v) = each %tagcorpus ) { #print "$k => $v\n"; }

      the key and value pairs are being printed properly.

      If this is solved the file write should work, I have used
      this same code elsewhere.What is the problem here?
      Thanx.

        I can't figure out why the print statement keeps printing the same word as a key value.

        The short answer is: Because that is what you asked it to do:)

        A slightly longer and hopefully helpful answer is that the keys function returns the keys from the hash. If you want to print the values from the hash you have two options. You can either use the key to retrieve the value from the hash

        %hash = qw/a 1 b 2 c 3 d 4 e 5/; for $key (keys %hash) { print 'Key:', $key, '=', $hash{ $key }, "\n" } Key: e = 5 Key: c = 3 Key: a = 1 Key: b = 2 Key: d = 4

        This is often done when you want to display the keys and the value as above, or perhaps when you want the values ordered by the keys.

        %hash = qw/a 5 b 4 c 3 d 2 e 1/; for $key (sort keys %hash) { print $hash{ $key }, "\n" } 5 4 3 2 1

        Here, the keys were sort ascending, but the values come out descending because thats the way they are associated.

        However, if you just want to print out the values and don't care about their order, or you want to order them by their own value, not that of their associated keys then you can use the values function instead.

        perl> %hash = qw/a 5 b 4 c 3 d 2 e 1/; for $val (values %hash) { print $val, "\n" } 1 3 5 4 2 for $val ( sort values %hash ) { print $val, "\n" } 1 2 3 4 5

        HTH.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller