> On Nov 3, 2015, at 2:03 PM, David Emanuel da Costa Santiago 
> <deman...@gmail.com> wrote:
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> 
> Hello all.
> 
> I'm trying to interpolate a hash value in a string but i got stuck.
> I already tried with eval, without any success… 

Show us what you tried.

> I have two conf files in the form
> conf file 1:
> option=value
> option2=value2
> 
> conf file 2:
> property=propertyValue
> property2=propertyValue2
> 
> I'm loading these files into a hash. But the problem is that i want to
> access, for example on conf file 2, properties from conf file 1:
> 
> property3=$HASH{option2}

is that literally what appears in file 2?

> So i'm getting the hash for conf file 2:
> 
> %PROPERTIES=(property=>"propertyValue",  property2=> "propertyValue2",
> property3=> "$HASH{option2}" );
> 
> but what i want is:
> %PROPERTIES=(property=>"propertyValue",  property2=> "propertyValue2",
> property3=> "value2" );
> 
> 
> this is how i'm reading the files:
> 
> sub _read_conf_file{
>  open my $FH, '<', shift @_;
>  while(<$FH>){
>    chomp;
>    s/\#.*//;
>    /(\S+)\s*=\s*(.+)/;
>    next if (!defined $1 || !defined $2);
>    $OPTS{$1}=$2;
>  }
>  close $FH;
> }
> 
> Does anybody knows how to do this?

Here is one way parsing each value with a regular expression:

#!/usr/local/bin/perl
use warnings;
use strict;

my %file1 = ( A => 'A', B => 'B' );

my %HASH;
_read_conf_file();

for my $key ( sort keys %HASH ) {
  print "$key = $HASH{$key}\n";
}

sub _read_conf_file{
  open my $FH, '<', shift @_;
  while(my $line = <DATA>) {
    next if $line =~ /^\s*#/;
    chomp($line);
    my( $key, $val ) = split(/=/,$line);
    next unless defined $val;

    if( $val =~ m{\$file1\{'(\w+)'\}} ) {
      $val = eval($val);
#     you could also use the following line here instead (with some loss of 
generality)
#     $val = $file1{$1};
   }
    $HASH{$key}=$val;
 }
}
__DATA__
A=$file1{'B'}


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to