-2

In the following code I am trying to use data from Input File 1 to edit data in Input File 2. But the problem is the code is not able to match or substitute when the possible match text is anywhere other than at the last, towards right.

Could you please help me in figuring out way to match it in the entire text.

I am still new to coding so if you find any other possible enhancements, i will highly appreciate your guidance.

Input File 1

Saint   st

Saint   saint

Saint   st.

Saint   snt

Saint   snt.

Hotel   htl

Hotel   htl.

 Road   rd

 Road   rd.

Input File 2

Part.Name.

Gordon house st

Gordon saint house

Gordon st. house

Gordon snt house

Gordon snt. house

htl palace

htl. Indiana

nuav rd hotel

dankei hotel rd.

Code Starts Here

use strict;
use warnings;
open (my $fh1, "< $filename1") or die $!;
my @incomin_data1=<$fh1>;
my $array_length1=$#incomin_data1;
my @key; my @value;
for (my $count=0;$count<=$array_length1;$count++)
{($key[$count],$value[$count])=split /,/,$incomin_data1[$count];}
my $key_length=$#key;
open (my $fh2, "< $filename2") or die $!;
my @incomin_data2=<$fh2>;
my $array_length2=$#incomin_data2;
for (my $count2=0;$count2<=$array_length2;$count2++)
{     for (my $count3=0;$count3<=$key_length;$count3++)
{     my $ky=$key[$count3];
my $val=$value[$count3];
if ($incomin_data2[$count2]=~/\s?$val\s?/g)
{     $incomin_data2[$count2]=~s/$val/$ky/;  }}}
print "\n\n",@incomin_data2;
Borodin
  • 126,100
  • 9
  • 70
  • 144
Tanumoy
  • 3
  • 4
  • You seem to be splitting on `,` data that doesn't have a comma anywhere. You don't need to iterate particularly though `s/search/replace/g` will work on a whole chunk of text at once. – Sobrique Feb 27 '15 at 14:44
  • this is actually from a csv file. So when extracted it does come with " , ". i tried using "g" it still didnt work. not sure why. – Tanumoy Feb 27 '15 at 14:56

1 Answers1

0

The best I can do for you is to simply write a solution. Your own code is irretrievable.

use strict;
use warnings;

my ($file1, $file2) = @ARGV;

my %abbrevs;

open my $fh, '<', $file1 or die $!;
while (<$fh>) {
  chomp;
  my ($phrase, $abbrev) = split /,/;
  if ( exists $abbrevs{$abbrev} ) {
    die sprintf 'Abbreviation "%s" already assigned to "%s"', $abbrev, $phrase;
  }
  $abbrevs{$abbrev} = $phrase;
}

my $re = join '|', map quotemeta, sort { length $b <=> length $a } keys %abbrevs;
$re = qr/$re/;

open $fh, '<', $file2 or die $!;
while (<$fh>) {
  s/(?<![\w.])($re)(?![\w.])/$abbrevs{$1}/g;
  print;
}

output

Part.Name.
Gordon house Saint
Gordon Saint house
Gordon Saint house
Gordon Saint house
Gordon Saint house
Hotel palace
Hotel Indiana
nuav Road hotel
dankei hotel Road
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • (?=a) for a lookahead, ?! for negative lookahead, or ?<= and ?<! for lookbehinds (positive and negative, respectively). – Tanumoy Mar 16 '15 at 14:19
  • Hi Borodin, while trying to understand your code i picked up the previous line. i would really appreciate if you could help me understand that. ?<![\w.])($re)(?![\w.]) – Tanumoy Mar 16 '15 at 14:21
  • Hi Borodin, your code helped me a lot. I have another question related to same =~/(?<![\w.])($val)(?![\w.])/gi). i am trying to match value of $val in target string. i have noticed a new problem. if $val is xxx. and is to be replaced by YY then the regex is picking every xxx@. where @ means any character and not just "." And i am sorry but cant remove the "." or any other nonalphanumeric character that comes in $val. requesting for help. – Tanumoy Mar 17 '15 at 15:58
  • @Tanumoy: Stack Overflow isn't a forum, and you shouldn't ask additional questions in comments. If you are still having problems that you can't resolve then you should post a new question. – Borodin Mar 18 '15 at 13:35
  • Thank you for the guidance Borodin. I am still understanding the way it works here. Just that i have been blocked by website to ask another question for next 2 days. and i still have questions. – Tanumoy Mar 18 '15 at 13:37
  • @Tanumoy: I have never heard of that happening. As far as I can tell you have only ever asked two questions, so I am surprised that you have been blocked. However you shouldn't try to get around the system, and I am sure that you could find your own solution if you spent those two days learning the language. – Borodin Mar 18 '15 at 13:40