Avinash Gopal
Avinash Gopal

Reputation: 101

Fetching data from a file using Perl

I have file as:

<country=HK>

    TCN=1
    CURR_TYPE="RS"
    PRICE=10
    COMP_NAME="IBM"

    TCN=2
    CURR_TYPE="RS"
    PRICE=200
    COMP_NAME="CTS"

    TCN=3
    CURR_TYPE="RS"
    PRICE=50
    COMP_NAME="TCS"


endHK

<country=JN>

    TCN=1
    CURR_TYPE="YEN"
    PRICE=10
    COMP_NAME="IBM"

    TCN=2
    CURR_TYPE="YEN"
    PRICE=200
    COMP_NAME="CTS"

    TCN=3
    CURR_TYPE="YEN"
    PRICE=50
    COMP_NAME="TCS"

</country=JN>

Now I want to retrieve the values from the members in above file using a Perl script.

My Perl script file is:

#!perl

open(FH, "<a.txt");
@a=<FH>;
$b=$#a;
for ($n=0;$n<$b;$n++)
{
    if ($a[$n]=~/HK/)
    {
        foreach $_ ( @a[$n..($n+300)])
        {               
            if($_ =~ /endHK/){ exit 0;}
            print $_;
        }
    }
}

close(FH);

Please append the code to help me retrieve the data from the above file.

Upvotes: 2

Views: 738

Answers (1)

Sebastian Stumpf
Sebastian Stumpf

Reputation: 2791

The file doesn't look to hard to parse, although the pasted code uses two different closing tags... (endHK and </country=JN>). A basic recipe for parsing simple data could look like this:

Retrieve the file:

use autodie;
open(FILE, '<', 'file.txt');
my @data = <FILE>;
close(FILE);

Loop over the contents of it:

my (%file, $country);
foreach my $line (@data) {

Remove unnecessary characters:

chomp $line;
$line =~ s/^\s+|\s+$//g;
next unless $line;

And build up some data structure:

if($line =~ m!^<country=([^>]+)>!) {
    $country = $1;
}
elsif($line =~ m!^([^<=]+)=(.+)$!) {
    my ($key, $value) = ($1, $2);
    $value =~ s/"//g;

    $file{$country}->{$key} = $value;
}

Verify the output:

print Dumper \%file;

This should print something like:

$VAR1 = {
          'HK' => {
                    'PRICE' => '50',
                    'CURR_TYPE' => 'RS',
                    'COMP_NAME' => 'TCS',
                    'TCN' => '3'
                  },
          'JN' => {
                    'PRICE' => '50',
                    'CURR_TYPE' => 'YEN',
                    'COMP_NAME' => 'TCS',
                    'TCN' => '3'
                  }
        };

Also: Have a look at Config::General. This module provides a "safer" way of dealing with such data.

Upvotes: 5

Related Questions