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

"It doesn't work" does not help us to help you - please provide any error messages you are getting, and/or describe how the program currently behaves and how you'd like it to behave.

Having said that, in your map you don't need the semicolon in /\|/;, since there is another argument to split ($_) following the regular expression, the statement doesn't end there. You said you are a Perl beginner, so have a look at perlintro (and perlsyn).

To help your further could you provide a sample of what the three files (2x input 1x output) currently look like, and an example of how you'd like them to look? You can also edit your original post to include this information, see How do I change/delete my post?

Replies are listed 'Best First'.
Re^4: How to substitute a word with an other?
by Anonymous Monk on May 17, 2014 at 18:13 UTC

    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

      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