in reply to File write

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


Replies are listed 'Best First'.
Re: Re: File write
by perl_seeker (Scribe) on Jun 27, 2003 at 11:27 UTC
    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


        Hello,
        what I meant was that this bit of code in the
        program(in the subroutine process)
        foreach $key(keys %tagcorpus){ print "\n$key"; return($key); }


        gives this as output(the same word as a key):
        else else else else else else else else else else else else else else


        whereas this bit of code
        while ( ($k,$v) = each %tagcorpus ) { print "$k => $v\n"; }


        prints the key&value pairs properly
        else => 1 on => 1 earth => 1 Here => 1 me => 1 am => 1 rather => 1 this => 1 Theres => 1 Id => 1 I => 1 is => 1 nowhere => 1 be => 1


        but the subroutine process keeps returning only this word: else

        Thanks for the help on hashes anyway.
        I have changed the code and it now works.
        Here is the changed file write portion:
        open CF, '+<c.txt' or die $!; seek CF, 0, 0; # go to start of file truncate CF, 0; foreach $citem(keys %tagcorpus){ #print "\n$citem"; print CF $citem; print CF " "; foreach $ditem(keys %tagdict){ my $currentposition=tell CF; seek CF,0,1 ; if ($citem eq $ditem){ print CF "$tagdict{$ditem}"; } } } close CF;


        This changes c.txt into(gramm. category against matched
        word)
        else on earth Here noun me am verb rather this Theres advb Id I is now +here be


        i.e noun against Here verb against am advb against Id


        Of course the words in the sentence are not properly
        ordered.For this I think we need to extract the keys of
        %tagcorpus in the insertion order and then write onto the
        file.This is what I need to do now :)
        Thanx.