http://www.perlmonks.org?node_id=850208


in reply to converting rows into column

You can achieve it by using the following code.For your requirement,I have stored the column names into array in hard coded manner.If you want to be dynamic, then you can store the column headings into hash.At the time, you can't expect the hash keys to be in order.First, you check this.If you face any problem, I will explain you with storing and retrieving the details from hash.

use strict; use warnings; my %hash; #declaring hash for storing the data open FH, "<input.txt" or die "can't open file:$!"; #opening the file i +n read mode my $i=0; #declaring iterator for storing different records in a file my @arr; #temporary array to store splitted details while(<FH>) { #reading data from file if(/^Artist:\s*(.*)$/) { #checking whether its first artist detail +s $i++; #incrementing the iterator for storing the details push @{$hash{$i}}, $1; #pushing the artist name to array while(<FH>) { #continue reading the file to get artist det +ails if(/^Artist:\s*(.*)$/) { #checking whether particular +artist details completed $i++; #incrementing the iterator for storing the d +etails push @{$hash{$i}}, $1; #pushing the artist nam +e to array last; #break the loop } else { @arr=split(':'); #splitting the field name and val +ue $arr[1]=~s/\s//g; #removing the unwanted space +s in file push @{$hash{$i}}, $arr[1]; #storing the furth +er informations into array } } } else { @arr=split(':'); #splitting the field name and value $arr[1]=~s/\s//g; #removing the unwanted spaces in file push @{$hash{$i}}, $arr[1]; #storing the further informati +ons into array } } my @fields=('Artist','Title','Album','Year','Genre'); #storing the fie +ld names in the file in order print "$_\t" for @fields; #printing the column headings print "\n-------\t-----\t------\t----\t-----\n"; #printing the ----- a +fter each column for my $key (sort keys%hash) { #getting the records in order from hash my $j=0; print $hash{$key}->[$j++]." " while($j<5); #getting all data of arti +st from hash and printing it print "\n"; #printing newline for differentiating the records }