Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitution 3

Status
Not open for further replies.

dreamstreet

Programmer
Jan 24, 2003
25
US
Hello,

I have a text file liek this:

3d=3do
1=one
hel=hello

One a page, I have a text field and if tehy type anything from the text file before the '=' sign, it should repleace it with whatever after the '=' sign'. For example, if they typed '3d', it should replace it with '3do'. If they type '1', it should replace it with 'one'. I have the following code but it is not working. If anyone can help, that'll be great.

open (FILE, "line.txt");
@line=<FILE>;
close(FILE);

@t= split(/=/, @line);

foreach $f (@t) {

if ($text =~/$f/) {
$trans=$f;
}

print $trans;
exit;
}
 
I'd be more inclined to use a hash here; something like:
[tt]
open F, 'line.txt';
while(<F>){/(.*)=(.*)/;$t{$1}=$2;}
close F;

$text='3d shell';

for(keys %t)
{
$text=~s/$_/$t{$_}/;
}

print $text;
[/tt]
 
Code:
foreach $f (@t) {
  if ($text =~/$f/) { #where is $text supposed to be coming from
    $trans=$f;        #where is $trans supposed to be coming from
  }
}
print
I'd probably tackle it like this
Code:
open FH, "<subs.txt" #file with substitutions
my %subs;            #create a hash
while (<FH>) {
  ($quest, $repl)=split /=/, $_;
  $subs{$quest}=$repl;   #build your hash structure, so your key is
                         #what your looking to replace, and the value
                         #is what you're looking to replace it with
}
close FH;                #substitution hash built
open FH2, "<file.txt";   #open file for examination
while (<FH2>) {
   (@data)=split /\s+/, $_; #split the line on whitespace
   for ($loop=0;$loop<=$#data;$loop++) {  #foreach item in the array
      if (exists $subs{$data[$loop]} {    #if a word exists in the hash
          $data[$loop]=$subs{$data[$loop]}; #replace it
      }
   }
   print join " ",@data;    #rejoin your array with spaces, and print
}
close FH2;

HTH
--Paul

cigless ...
 
Note that you're reading the contents of FILE into array @line, and then trying to split that array into another array, @t. You can't split an array. You can split a string.

But a hash is much better for this:
Code:
#!perl
use strict;
use warnings;

open (FILE, "line.txt") || die qq(Can't open "line.txt" for input!\n);
my %h = map {chomp; split /=/} <FILE>;
close(FILE) || die qq(Can't close "line.txt" after read!\n) ;

print ">>";
chomp(my $text = <STDIN>);
while ($text) {
    if (exists($h{$text})) {
        print "$h{$text}\n";
    } else {
        print "NO MATCH\n";
    }
    print ">>";
    chomp($text = <STDIN>);
}
exit(0);
If you really must do it with an array instead of a hash:
Code:
#!perl
use strict;
use warnings;

open (FILE, "line.txt") || die qq(Can't open "line.txt" for input!\n);
my @t = map {chomp; [ split /=/ ]} <FILE>;
close(FILE) || die qq(Can't close "line.txt" after read!\n) ;

print ">>";
chomp(my $text = <STDIN>);
while ($text) {
    if (my @trans = grep {$_->[0] eq $text} @t) {
        print "$_->[1]\n" for @trans;
    } else {
        print "NO MATCH\n";
    }
    print ">>";
    chomp($text = <STDIN>);
}
exit(0);
As you can see, the hash version is simpler.

HTH
 
That's three of us with nothing better to do on a Sunday evening ;-)
--Paul

cigless ...
 
Thank you for the lengthy code. I appreciate that.

"That's three of us with nothing better to do on a Sunday evening"

I figured...I think that's how tek-tips.com started :p
 
TonyGroves,

I tried your first code. It did worked but one problem is some of them has spaces on it like:

h=hello for now
ty=thank you
3d=3do

When it has spaces, i don't think it will work. Any idea? Thanks.
 
PaulTEG:
Dublin!

Dreamstreet:
I don't see why it wouldn't work with those strings. Just one thing though, that code I posted is very very basic, just a suggestion as to how the problem might be approached. It would most likely need a lot of improvement to make it suitable for real-world use.
 
Down the road in Naas, the Fair City bit threw me

--Paul

cigless ...
 
Code:
BEGIN {FS="="}
NR==FNR {aa[$1]=$2; next}
$0 in aa {$0=aa[$0]}
1
[tt]awk -f sub.awk line.txt -[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top