I have a txt file like this
$CHUNK_QTY = "50000";
$CLIENT = "hi all";
$CLIENT_CODE = "NMB";
$COMPOSER = "DIALOGUE";
$CONTROL_FILE_NAME = "NMBM725.XML";
$COPY_BASE = "01";
$CSITE = "NSH";
$DATA_TYPE = "MET";
$DIALOGUE_VERSION = "V7R0M624";
$DISABLE = "N";
$DPI = "300";
$DP_BAR_START = "A";
$DP_BAR_STOP = "Z";
$DUPLEX = "N";
$Dialogue_Version = "V7R0M624";
$EMAIL_ERROR = "Y";
$EMAIL_ON = "N";
I have many variables up to 500. I would like to access value for corresponding variable. For example if I want to access $DPI it should print 300 . How do I do that in perl. Any help will be appreciated. I would like something different than regex.
Thanks
Here's a quick example showing how to take lines in the format that you gave above and put them into a hash:
#!/usr/bin/env perl
use strict;
use warnings;
my %vars;
while (my $line = <DATA>) {
chomp $line; # remove linebreak
$line =~ s/^\$//; # Optional: remove $ from start of variable name
my ($key, $value) = $line =~ /^(\w+)\s*=\s*(.*)$/;
$value =~ s/;$//; # Remove trailing semicolon
$value =~ s/^"//; # Remove leading double-quotes
$value =~ s/"$//; # Remove trailing double-quotes
$vars{$key} = $value;
}
for my $key (sort keys %vars) {
print "$key has value $vars{$key}\n";
}
print "CLIENT says $vars{CLIENT}\n";
__DATA__
$CHUNK_QTY = "50000";
$CLIENT = "hi all";
$CLIENT_CODE = "NMB";
$COMPOSER = "DIALOGUE";
$CONTROL_FILE_NAME = "NMBM725.XML";
$COPY_BASE = "01";
$CSITE = "NSH";
$DATA_TYPE = "MET";
$DIALOGUE_VERSION = "V7R0M624";
$DISABLE = "N";
$DPI = "300";
$DP_BAR_START = "A";
$DP_BAR_STOP = "Z";
$DUPLEX = "N";
$Dialogue_Version = "V7R0M624";
$EMAIL_ERROR = "Y";
$EMAIL_ON = "N";
There should be enough here to get you started, but you'll need to read up on how to open an actual file (instead of using the __DATA__
section as I did here) for yourself. I recommend checking perldoc open
for examples and full details.