I am trying to parse text file content consists of 3 categories and access it in the main code. I got to know that hash maybe a good way but since no columns in the input file is unique (Name could be repeatedly or different), I doubt is there other way to do it. Appreciate any reply.
#!/usr/bin/perl
use strict;
use warnings;
my $file = "/path/to/text.txt";
my %info = parseCfg($file);
#get first line data in text file (Eric cat xxx)
#get second line data in text file (Michelle dog yyy)
#so on
}
sub parseCfg {
my $file = shift;
my %data;
return if !(-e $file);
open(my $fh, "<", $file) or die "Can't open < $file: $!";
my $msg = "-I-: Reading from config file: $file\n";
while (<$fh>) {
if (($_=~/^#/)||($_=~/^\s+$/)) {next;}
my @fields = split(" ", $_);
my ($name, $son, $address) = @fields;
#return something
}
close $fh;
}
Input file format:(basically 3 columns)
#Name pet address
Eric cat xxx
Michelle dog yyy
Ben horse zzz
Eric cat aaa