in reply to How to substitute a word with an other?

Take a look at the documentation of split, all you would need to do to support a different character is change that line to something like this: my @line = split /\|/; (note the backslash is required because normally the pipe character is a special character in regular expressions, see perlre). An alternative way to write that is /\Q|/, the \Q causes the characters following it to lose their special meaning (documented in quotemeta).

With XML things start getting more involved, for starters you'll need an XML module such as XML::Simple or XML::Twig to parse the XML, and then you'll need to walk through the data structures. If you're a beginner then perldsc might be a good place to start, as well as all the other tutorials that are a part of the Perl documentation - perlintro.

Replies are listed 'Best First'.
Re^2: How to substitute a word with an other?
by fx3000 (Initiate) on May 16, 2014 at 15:02 UTC

    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!

      "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