in reply to Re^3: How to substitute a word with an other?
in thread How to substitute a word with an other?

Hi, sorry, I'll try to be more detailed: I have a file we can call it Input.txt like the following

Input.txt 2014-04-24 14:22:52|TESTER001||/dev/pts/5|322655663|TYXRRES|0 2014-04-24 14:22:52|TESTER003||/dev/pts/5|323231258|TRRRDER|0 2014-04-24 14:22:52|TESTER002||/dev/pts/5|323135368|RCVDDER|0

And I need to operate a secific substitutions using a "map" the script can found in Changes.txt

Changes.txt TESTER001,Frank_Sinatra TESTER002,Jhon_Belushi TESTER003,Homer_Simson

After the changes the output should look like this:

Output.txt (desired) 2014-04-24 14:22:52|Frank_Sinatra||/dev/pts/5|322655663|TYXRRES|0 2014-04-24 14:22:52|Homer_Simson||/dev/pts/5|323231258|TRRRDER|0 2014-04-24 14:22:52|Jhon_Belushi||/dev/pts/5|323135368|RCVDDER|0

but the real Output.txt is different from my idea:

real Output.txt 2014-04-24 14:22:52 Frank_Sinatra /dev/pts/5 322655663 TYXRRES 0 2014-04-24 14:22:52 Homer_Simson /dev/pts/5 323231258 TRRRDER 0 2014-04-24 14:22:52 Jhon_Belushi /dev/pts/5 323135368|RCVDDER|0

So what I got is the right substitution but even an extra substitution of the | with a white space..... Here hte code i use

#!/usr/bin/perl use warnings; use strict; open( my $out_fh, ">", "Output.txt" ) || die "Can't open the output fi +le for writing: $!"; open( my $address_fh, "<", "Changes.txt" ) || die "Can't open the Chan +ges file: $!"; my %lookup = map { chomp; split( /,/, $_, 2 ) } <$address_fh>; open( my $file_fh, "<", "Input.txt" ) || die "Can't open the Input.txt + file: $!"; while (<$file_fh>) {

I think my problem is in the following Line...

my @line = split( /\|/, $_,); for my $char ( @line ) { ( exists $lookup{$char} ) ? print $out_fh " $lookup{$char} " : + print $out_fh " $char "; } print $out_fh "\n"; }

Thank you

Replies are listed 'Best First'.
Re^5: How to substitute a word with an other?
by morgon (Priest) on May 17, 2014 at 18:31 UTC
    my @input = split /\|/, $_; # we've split the input line into fields + - the "|" are gone now my @output = @input; $output[1] = $lookup { $output[1] } // $output[1]; #map the second fi +eld print $out_fh join("|", @output); # put the "|" back in
    CAUTION: untested