I have an input file
inputfile.txt
name: George
age: 5
nature: curious
likes: banana
This is what I call a form layout. I am trying to convert this in to a table layout, CSV. For instance:
name,age,nature,likes
George,5,curious,banana
So, I read the file, split on ": and \n" and place values in to a hash. Then push that hash in to an array so I can take them out later. Here is what I have done so far.
#!/usr/bin/perl
use strict;
open (MYFILE, 'inputfile.txt');
my @records;
while (<MYFILE>) {
chomp;
my %values = split(/[:\n]/,$_);
push @records,%values;
}
close (MYFILE);
By this, I guess @records={[name=George],[age=5],[nature=curious],[likes=banana]} would have happened.
Now, how do I get each hash out of the array @records? when I try something like:
foreach my $record(@records){
my %record = $record;
for my $key(keys %record){
print "key : $key\n";
}
}
It outputs all tokens one after other unlike what is expected (just the key).