CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Writing to file

 



msds
User

Apr 11, 2003, 2:57 AM

Post #1 of 7 (668 views)
Writing to file Can't Post

Smile Hello.

Consider this code:

Code
open CF,'d.txt' or die $!;            
while(<CF>){
chomp;
push @dwords,split;

}

$ldwords=@dwords;
#print "\n$ldwords";


for($i=0;$i<$ldwords;$i++)
{
#print "\nThis is the next word:";
#print "\n$dwords[$i]";
}

close CF;

open CP,'>c.txt' or die $!;
while(<CP>){
chomp;
push @cwords,split;
#print "\n$cwords[0]";
#print "\n$cwords[1]";
#print "\n$cwords[2]";
#print "\n@cwords";

}

$lcwords=@cwords;
#print "\n$lcwords";

#print "\nThese are the words:";

for($j=0;$j<$lcwords;$j++)
{

#print "\n$cwords[$j]";
}


seek CP, 0, 0;
truncate CP, 0;


for($j=0;$j<$lcwords;$j++)
{
for($i=0;$i<$ldwords;$i++)
{

if ($cwords[$j] eq $dwords[$i])
{
$categ=$dwords[$i+1];
#print "\nHere it is:";
#print "\n$categ";
#print "\n$dwords[$i]";

$categ=~ tr/noun/<no>/d;
$categ=~ tr/verb/<ve>/d;
$categ=~ tr/adverb/<ad>/d;


#print "\nHere it is:";
#print "\n$categ";

my $curpos=tell CP;
seek CP,0,1 ;
print CP $categ;

}
}
}

close CP;


c.txt:

Quote
Here I am this is me. There's nowhere else on
earth I'd rather be


d.txt:

Quote
Here
noun
am
verb
There's
adverb


I'm comparing the words in c.txt and d.txt.

A match would be found for 'Here', 'am', 'There's'.Then I'm
extracting the gram. category of the word from d.txt(The
categ. of each word would be stored in the array location next
to the location where the word is stored.)

Then I need to write the categ. onto c.txt.
That is after I run the code c.txt should look like:

Quote
Here <no> I am <ve> this is me. There's <ad> nowhere else on
earth I'd rather be



This code simply erases everything in c.txt.I've opened c.txt
in read mode and checked whether $categ is being extracted
properly.This part is O.K.


Code
       $categ=$dwords[$i+1]; 
#print "\nHere it is:";
#print "\n$categ";
#print "\n$dwords[$i]";


But the file write is not working. Where could the
problem be?Unsure

Thanks in advance,
msdsSmile


davorg
Thaumaturge / Moderator

Apr 11, 2003, 3:53 AM

Post #2 of 7 (665 views)
Re: [msds] Writing to file [In reply to] Can't Post


In Reply To

Code
open CP,'>c.txt' or die $!;                  
while(<CP>){
chomp;
push @cwords,split;
#print "\n$cwords[0]";
#print "\n$cwords[1]";
#print "\n$cwords[2]";
#print "\n@cwords";
}



You're opening CP in write-only mode and then trying to read from it. If you had "use warnings" turned on then Perl would have told you what was going on. Without "use warnings" Perl silently fails to read any data from the filehandle.


In Reply To


Code
$categ=~ tr/noun/<no>/d;    
$categ=~ tr/verb/<ve>/d;
$categ=~ tr/adverb/<ad>/d;



That really doesn't do what you think it does either. Or, if it does, it's by a sheer fluke. I think you want s/// here.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


msds
User

Apr 22, 2003, 4:15 AM

Post #3 of 7 (650 views)
Re: [davorg] Writing to file [In reply to] Can't Post

Smile Hello Dave!

I've opened the file c.txt twice now, once for reading
and then for writing.It works just fine now.Also I've used
s// instead of tr.Thanks a lotCool

Here's the code:

Code
open CF,'d.txt' or die $!;            
while(<CF>){
chomp;
push @dwords,split;
}

$ldwords=@dwords;
#print "\n$ldwords";


for($i=0;$i<$ldwords;$i++)
{
#print "\nThis is the next word:";
#print "\n$dwords[$i]";
}

close CF;

open CP,'c.txt' or die $!;
while(<CP>){
chomp;
push @cwords,split;
#print "\n$cwords[0]";
#print "\n$cwords[1]";
#print "\n$cwords[2]";
#print "\n@cwords";

}
close CP;

$lcwords=@cwords;
#print "\n$lcwords";

#print "\nThese are the words:";

for($j=0;$j<$lcwords;$j++)
{

#print "\n$cwords[$j]";
}

open CP,'>c.txt' or die $!;
seek CP, 0, 0;
truncate CP, 0;

for($j=0;$j<$lcwords;$j++)
{
$_ = process($_);
print CP;
print CP " ";

for($i=0;$i<$ldwords;$i++) {
my $currentposition=tell CP;
seek CP,0,1 ;

if ($cwords[$j] eq $dwords[$i]) {
$categ=$dwords[$i+1]; #print "\nHere it is:";
#print "\n$categ";
#print "\n$dwords[$i]";
$categ=~ s/noun/<nou>/;
$categ=~ s/verb/<ver>/;
$categ=~ s/advb/<adv>/;


#print "\nHere it is:";
#print "\n$categ";

print CP $categ;
print CP " ";

}
}
}

close CP;

sub process {
return($cwords[$j]);
}
1;





I have another query.If the files c.txt and d.txt have a huge no. of words, say 60,000 words each.Then this script loops
indefinitely(hangs).

Is it these two loops causing the problem?

Quote
for($j=0;$j<$lcwords;$j++)
{
$_ = process($_);
print CP;
print CP " ";

for($i=0;$i<$ldwords;$i++) {
my $currentposition=tell CP;
seek CP,0,1 ;


How can this be solved?
thanx,
msdsSmile


(This post was edited by msds on Apr 22, 2003, 4:24 AM)


davorg
Thaumaturge / Moderator

Apr 22, 2003, 4:22 AM

Post #4 of 7 (648 views)
Re: [msds] Writing to file [In reply to] Can't Post

You almost certainly want to replace @dwords with a hash where the key is the word and the value is the category.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


msds
User

Apr 30, 2003, 3:57 AM

Post #5 of 7 (635 views)
Re: [davorg] Writing to file [In reply to] Can't Post

Smile
Hello Dave.Back to this after quite some time.
And with another queryBlush

Am trying the hash thing with d.txt:

Code
open CF,'d.txt' or die $!;                 
my %tagdict;

while(<CF>){
chomp;
push @dwords,split;
#print "\n@dwords";
}

@dcateg;

$ldwords=@dwords;
#print "\n$ldwords";

for($i=0;$i<$ldwords;$i++){

for($n=1;$n<3;$n++){
$dcateg[$i]=$dwords[$i+$n];
#print "\n$dwords[$i]";
print "\n$dcateg[$i]";

}
}
#print "\n@dcateg";

@tagdict{@dwords} = @dcateg;

while ( ($k,$v) = each %tagdict ) {
print "$k => $v\n";
}


Consider this print st.:

Quote
print "\n$dcateg[$i]";

This prints:
noun
am
am
verb
verb
Theres
Theres
advb
advb


I need to store these in @categ:
noun
verb
advb

so that the hash print st. later gives
Here => noun
am =>verb
Theres =>advb

Where is the mistake?Unsure
thanx,
msdsSmile


davorg
Thaumaturge / Moderator

Apr 30, 2003, 4:11 AM

Post #6 of 7 (633 views)
Re: [msds] Writing to file [In reply to] Can't Post

What is the format of the data in d.txt? I've been assuming that it consists of lines containing one word and its associated category.

If that's the case then I'd build the hash like this:

Code
open CF, 'd.txt' or die $!; 
my %tagdict;

while (<CF>) {
chomp;
my ($word, $cat) = split;
$taqdict{$word} = $cat;
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


msds
User

May 1, 2003, 3:37 AM

Post #7 of 7 (614 views)
Re: [davorg] Writing to file [In reply to] Can't Post

Smile Hello Dave.

As usual, ur code works just fine Cool

The file d.txt actually had one word per line,

Quote
Here
noun
am
verb
Theres
advb


I've changed it to your format.


Quote
Here noun
am verb
Theres advb


I've stored the words in c.txt in a hash too, and then
found the keys common to both hashes.

It works fine till here.

Code
open CF, 'd.txt' or die $!; 
my %tagdict;
while (<CF>) {
chomp;
my ($word, $cat) = split;
$tagdict{$word} = $cat;
}

while ( ($k,$v) = each %tagdict ) {
#print "$k => $v\n";
}

open CP, 'c.txt' or die $!;
my %tagcorpus;

while (<CP>) {
chomp;
my @cwords =split;

#$lcwords=@cwords;
#for($i=0;$i<$lcwords;$i++){
#$cwords[$i]=~ s/,/ /d;
#$cwords[$i]=~ s/'/ /d;
#$cwords[$i]=~ s/./ /d;
#}

@tagcorpus{@cwords} = (1) x @cwords;
}

while ( ($k,$v) = each %tagcorpus ) {
#print "$k => $v\n";
}

my @common=();
foreach (keys %tagdict){
push(@common,$_) if exists $tagcorpus{$_};
}

#print "\n@common";

foreach $citem(@common){
#print "\n$tagdict{$citem}";
}

close CP;


Quote
The st.

print "\n$tagdict{$citem}";

gives this

advb
noun
verb

which reminds me that I don't have the categories of
the words in the order they were inserted into the hash.
I guess that can be taken care of.

I've also removed commas, periods, quotes in c.txt for
the time being.But I need to do away with them within the
script. Tried some s// stmts which didn't work.

But the trickiest part of all isCrazy writing the category
against the appropriate word in c.txt.

c.txt needs to look like this:

Here<noun> I am<verb> this is me Theres<adverb> nowhere else on earth Id rather be

My earlier file write code seems too long and clumsy to
use with hashes.

How would you do this? Am waiting for your reply.
thanx,
msdsSmile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives