sagar_qwerty has asked for the wisdom of the Perl Monks concerning the following question:
I have script like this:
open(INFILE,"<","words.txt") my @X; while(<INFILE>){ push @X,(split(/\s+/,$_))[0]; ### choosing particluar column and pu +shing all its values in single array } $a = 0; $main = 'temp.txt'; $mod = 'temp_mod.txt'; for (0..$#X) { $b = $main; open b ; open NEWFILE1, ">$mod" ; while (<b>) { / $X[$a] / or print NEWFILE1 } close NEWFILE1; $main = $mod; $mod = $b; $a++; }
What it is doing: it is removing lines having "words" in single column in words.txt file. Which are stored in array @X. It opens file temp.txt, removes first word and save it in file temp_mod.txt. this later file is used again to remove second word from @X array and this loop runs by opening and closing temp.txt and temp_mod.txt
I have lot many words and many lines in file (60mb). So opening, removing line, closing, opening cycle consumes lot of time. Can I just open once a file and remove all lines together having words stored in words.txt
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to avoid opening and closing files
by davido (Cardinal) on Jun 18, 2012 at 05:29 UTC | |
|
Re: how to avoid opening and closing files
by zentara (Cardinal) on Jun 18, 2012 at 10:55 UTC | |
|
Re: how to avoid opening and closing files
by cheekuperl (Monk) on Jun 18, 2012 at 04:01 UTC | |
|
Re: how to avoid opening and closing files
by pvaldes (Chaplain) on Jun 18, 2012 at 18:19 UTC |