For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > Load an hash from a text file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Load an hash from a text file
Web Solving

2005-02-28, 8:55 am

Hi all, i'm a real perl newbie. i've a question for you all. i've to
perform a "search and replace" on a text file, and i wrote this:
open(READIT,"file.txt") || die "Cant read file.txt. Reason: $!";
open(WRITEIT,">>file_new.txt") || die "Cant write file_new.txt. Reason: $!";
while (<READIT> ) {

if ($riga =~ /una pallina/) {
$riga =~ s/una pallina/<a href="http:/\/\www.pollino.it>una pallina</a>/;
}
elsif ($_ =~ /pallina/) {
$riga =~ s/pallina/<\a href="http:\/\/www\.pollino.it>pallina<\/\a>/;
}

if ($riga =~ /un cobra/) {
$riga =~ s/un cobra/<a href="http:/\/\www.serpentoni.it>un cobra</a>/;
}
elsif ($_ =~ /cobra/) {
$riga =~ s/cobra/<\a href="http:\/\/www\.serpentoni.it>cobra<\/\a>/;
}

print WRITEIT "$riga\n";

}
close(WRITEIT);
close(READIT);

i need to load the corrispondence "world to be s"=>"replaced word"
from another text file, like this:
word|<\a href="http:\/\/www\.word.it>word<\/\a>
the word|<\a href="http:\/\/www\.word.it>the word<\/\a>
a word|<\a href="http:\/\/www\.word.it>a word<\/\a>
word2|<\a href="http:\/\/www\.word2.it>word2<\/\a>
the word2|<\a href="http:\/\/www\.word2.it>the word2<\/\a>
a word2|<\a href="http:\/\/www\.word2.it>a word2<\/\a>
eccetera.....

so i think i can use an hash, but i can't
understand how i can load the txt file in an hash, i tried to search
the answer of my question in several tutorials but i can't find
anything about it. can u help me?
--
WebSolvingJaa: informatica per il web e l'azienda.
http://websolvingjaa.altervista.org/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com