in reply to Re: Need to replace string from external file
in thread Need to replace string from external file

HI corion

I have written this code

use strict; use warnings; use Path::Tiny qw(path); my $filenme='d:\SampleCiscoIOSEvent.txt'; my $file=path($filenme); my $data= $file->slurp_utf8; use File::Slurp; my $filename='e:\filename.txt'; my @arr = read_file($filename); my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; print @output;

the problem is it is working fine for one variable But if i have more then 1 variable my output is very confusing Please help me

My code for changing more then one variable is as follow

use strict; use warnings; use Path::Tiny qw(path); my $filenme='d:\SampleCicoIOSEvent.txt'; my $file=path($filenme); my $data= $file->slurp_utf8; use File::Slurp; my $filename='e:\filename.txt'; my $IPfilename='e:\IPfilename.txt'; my @arr = read_file($filename); my @arr1 = read_file($IPfilename); my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; my @output1= map{ $data=~s/IP/$_/rg} @arr1; print @output; print @output1;

Replies are listed 'Best First'.
Re^3: Need to replace string from external file
by Corion (Patriarch) on Nov 06, 2017 at 11:49 UTC
    my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; my @output1= map{ $data=~s/IP/$_/rg} @arr1;

    This is almost correct, but note that you are storing the first transformation, using C_USERNAME in @output, and then the second transformation, using IP in @output1. Most likely, you will want to transform everything, after having replaced all C_USERNAME and all IP.

    Instead of reusing $data in the second output, you will likely want to use the elements in @output.

    my @usernames = map { $data =~ s/C_USERNAME/$_/rg } @arr; my @user_and_ip; for my $user_string (@usernames) { push @user_and_ip, map { $user_string =~ s/IP/$_/rg } @arr1; }

    A better approach would likely be to use a templating engine over your data.

      Hey corion Thanks a ton.Your code somewhat help me .
      brother i cannot see use of external file in your program for storing the replacment value.. I am just a beginner Need spoon feeding. You help will be highly appreciated. I have created this program but just doing R& D on google .

        I think the best approach is to split up your problem into several parts:

        1. Reading input files - you already have this using Path::Tiny
        2. Correlating the input data and replacing the input data in the template - this part maybe needs some more clarification but basically also works
        3. Writing the resulting output - again, Path::Tiny offers a way to write the files

        Regarding issue 2, the input data: You haven't been explicitly clear about this, but I think from your statements that the first line of the users file corresponds to the first line of the IP addresses file. If that is the case, using a templating engine is even more advised, like HTML::Template, but you can also do with the simplest templating engine, s///e:

        sub fill_template { my( $template, %values ) = @_; my $re_values = join "|", map { "\b$_\b" } reverse sort keys %valu +es; $template =~ s!($re_values)!exists $values{ $1 } ? $values{ $1 } : + $1!gre; } for my $linenumber (0..$#arr) { my $user_data = fill_template( $data, USER_C => $arr[$i], IP => $a +rr1[$i] ); print $user_data; # well, output to a file, but that's another pro +blem }

        Update: s/Path::Class/Path::Tiny, spotted by marto

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^3: Need to replace string from external file
by bhupi70007 (Novice) on Nov 06, 2017 at 11:47 UTC
    This is the second program that i created for replacing two argument. But it is getting complex
    use strict; use warnings; use Path::Tiny qw(path); my $filenme='d:\SampleCicoIOSEvent.txt'; my $file=path($filenme); my $data= $file->slurp_utf8; use File::Slurp; my $filename='e:\filename.txt'; my $IPfilename='e:\IPfilename.txt'; my @arr = read_file($filename); my @arr1 = read_file($IPfilename); my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; my @output1= map{ $data=~s/IP/$_/rg} @arr1; print @output; print @output1;