Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: converting rows into column

by nvivek (Vicar)
on Jul 19, 2010 at 06:04 UTC ( #850208=note: print w/replies, xml ) Need Help??

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 }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://850208]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-12-09 02:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found