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

Hi thanks for ur answer, but if I try to substitute the line

my @line = split;

with this one :

my @line = split /\|/;;

I just got in the output file the | is removed but it's not this my goal. I try to use your suggestion in the previous line:

my %lookup = map { chomp; split( /\|/;, $_) } <$address_fh>

but doesn't work Thanks for your help!

Replies are listed 'Best First'.
Re^3: How to substitute a word with an other?
by Anonymous Monk on May 16, 2014 at 15:27 UTC

    "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?

      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