Hi i am new to Perl programming. I am trying to read a .csv
file which has 2 fields separated by comma.
I want to put all the data of a file into a hash in the form of key and its value.
The input file I have is
Data
2.8, gitu 2.5, Has 2.7 Hwait 3.1-weiity 4.2, city 2.7:query 4.9, city 16.2, play 6.2, game 7,,,8 Jami 4.0, city
This line of code does not print all the valid data present in the input file. Valid data is in the form of
a line start with a number have a comma in between and then a name. Else invalid entries should be ignored. It skips few valid entries to display when I print %hashforHighMagnitude;
Please tell me where I am missing ? How can I get all the valid entries present %hashforHighMagnitude;
I am trying to do this
open ( OF, "$inputFile") or die "Cant open input file: $!\n";
while ( $Line =<OF>) {
if($Line =~ /^\d+\.+\d*\s*,\s*\w+$/g)
{
( my $magnitude, my $place ) = split(/,/,$Line);
$hashforHighMagnitude{$place} = $magnitude;
$hash{$place}++;
}
else
{
next;
}
}
print %hashforHighMagnitude;
close(OF);
output should be
2.8, gitu 2.5, Has 4.2, city 4.9, city 16.2, play 6.2, game 4.0, city