Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Need to replace string from external file

by Corion (Patriarch)
on Nov 06, 2017 at 11:49 UTC ( [id://1202822]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: Need to replace string from external file
by bhupi70007 (Novice) on Nov 07, 2017 at 07:03 UTC
    Hey corion Thanks a ton.Your code somewhat help me .
Re^4: Need to replace string from external file
by bhupi70007 (Novice) on Nov 06, 2017 at 12:02 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202822]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found