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

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 .
  • Comment on Re^4: Need to replace string from external file

Replies are listed 'Best First'.
Re^5: Need to replace string from external file
by Corion (Patriarch) on Nov 06, 2017 at 12:40 UTC

    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.