jimmydean has asked for the wisdom of the Perl Monks concerning the following question:

Hey Perl Monks, I have been searching for the last few days and I'm unable to find the solution. I'm trying to take in a java properties file (plan txt file) and find/replace the values with ones I will pull in from another file. Sounds easy... well my head hurts. I was hoping someone could help.
template.properties -------------------- stree.address=#enter address# city.name=place sate.name=unk current.txt -------------------- street.address=1234_ave_west city.name=seattle state.name=wa ####################CODE############################# open (templateFile, "C:/template.properties") or die "Could not open file: $!"; open (currentValues, "C:/current.txt") or die "Could not open file: $!"; foreach $line (<currentValues>) { ($k, $v) = split /=/, $line; foreach $tmpLine(<templateFile>) { print $k; if (m/$k/) { print $line; } else { print $tmpLine; } } } close templateFile; close currentValues;

Replies are listed 'Best First'.
Re: Find and replace from two files
by 2teez (Vicar) on Aug 18, 2012 at 06:13 UTC
    Hi,

    You can use hash like so:

    use warnings; use strict; use Data::Dumper; my %property_para; open my $fh, '<', "temp.txt" or die "can't open file: $!"; while ( defined( my $line = <$fh> ) ) { chomp $line; my ( $property, $value ) = split /=/, $line; $property_para{$property} = $value; } close $fh or die "can't close: $!"; open $fh, '<', "current.txt" or die "can't open file: $!"; while ( defined( my $line = <$fh> ) ) { chomp $line; my ( $property, $value ) = split /=/, $line; $property_para{$property} = $value if exists $property_para{$prope +rty}; } close $fh or die "can't close: $!"; $Data::Dumper::Varname = 'ADDRESS'; print Dumper \%property_para; # OR my %property_name; open_do_work( 'temp.txt', sub { $property_name{ $_[0] } = $_[1] } ); open_do_work( 'current.txt', sub { $property_name{ $_[0] } = $_[1] if $property_name{ $_[0] } } + ); $Data::Dumper::Varname = 'ADDRESS'; print Dumper \%property_name; sub open_do_work { my ( $file, $coderef ) = @_; open my $fh, '<', $file or die "can't open : $!"; while ( defined( my $line = <$fh> ) ) { chomp $line; my ( $property, $value ) = split /=/, $line; $coderef->( $property, $value ); } }

Re: Find and replace from two files
by Athanasius (Archbishop) on Aug 18, 2012 at 08:28 UTC

    Hello jimmydean, and welcome to the Monastery!

    First, ++2teez for the answer using a hash. That is the correct way to go.

    However, I thought you might like some feedback on why your code isn’t working:

    1. If you had included use warnings at the head of your script, you would have seen:

      Name "main::v" used only once: possible typo at ... line ... Use of uninitialized value $_ in pattern match (m//) at ... line ..., +<templateFile> line 1. street.addressstreet.address=#enter address# city.name=place state.nam +e=unknown

      The second warning shows what is wrong: Because you are using a named variable $tmpLine in the inner foreach loop, the default variable $_ is uninitialized. Change the regex-matching line to:

      if ($tmpLine =~ /$k/)
    2. The inner foreach loop reads to the end of the file “template.properties”, so on the next iteration of the outer loop you need to reset the filehandle’s position to the beginning of the file:

      foreach $line (<currentValues>) { ($k, $v) = split /=/, $line; seek(templateFile, 0, 0); # <== ADD THIS LINE foreach $tmpLine(<templateFile>) ...

      See seek.

    3. The logic of the two loops is still wrong. You need to search the file “template.properties” for a match, and then, when you’ve finished searching (either because you found a match, or because you read through to the end of the file), output the match (if found) or the original (if no match was found).

    Lastly, let me highlight some of the Perl best practices you will see in 2teez’s code:

    • Always begin a script with:

      use strict; use warnings;
    • Prefer lexical filehandles:

      open(my $templateFile...)
    • Prefer the 3-argument form of open:

      open(my $currentValues, '<', 'current.txt') or die ...

    Hope that helps,

    Athanasius <°(((><contra mundum

      2teez & Athanasius,

      Thank you so much for you help! I can't wait to try this out on Monday when I get back to work. I was wondering why people were using the
      use strict; use warnings;
      then Anonymous Monk posted some very helpful links. I hope to be able to help out others like you have helped me.

      Thank you all!

      J
Re: Find and replace from two files
by Anonymous Monk on Aug 18, 2012 at 03:43 UTC
Re: Find and replace from two files
by Kenosis (Priest) on Aug 18, 2012 at 15:45 UTC

    Here's another option using Tie::File:

    use Modern::Perl; use Tie::File; tie my @newProps, 'Tie::File', 'new_properties.txt' or die $!; my %newProps = map { /([^=]+)/ => $_ } @newProps; untie @newProps; tie my @oldProps, 'Tie::File', 'old_properties.txt' or die $!; map { /([^=]+)/; $newProps{$1} and $_ = $newProps{$1} } @oldProps; untie @oldProps;

    The first map iterates through @newProps, and uses a regex to capture each line's key to build a hash of keys/file lines. The second map iterates through @oldProps, and uses a regex to capture each line's key and, if it exists in the hash, replaces the old property line with the new property line. Changes are automatically written out to the old properties file.

    Hope this helps!