Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

converting rows into column

by changma_ha (Sexton)
on Jul 18, 2010 at 13:10 UTC ( #850154=perlquestion: print w/replies, xml ) Need Help??

changma_ha has asked for the wisdom of the Perl Monks concerning the following question:

i have a Pmp3 tag file like in text file ; Artist: aaa Title: bbb Album :hhh Artist : fuxt Title : Hum Album : por i want to output in the form: Artist Title Album ------ ----- ----- aaa bbb hhh fuxt Hum por Thanks in advance .....I m not able to convert

Replies are listed 'Best First'.
Re: converting rows into column
by biohisham (Priest) on Jul 18, 2010 at 13:40 UTC
    To be able to avail of the benefit of the collective experience that the monks have and take a rewarding educational leap you've got to make sure you provided enough details representing your problem communicated through :
    • Clear language.
    • Input example.
    • Required output form/example.
    • your coding attempts (succeeded or partially succeeded or totally flipped).
    That way you will be able to identify strengths and weaknesses in your approach, patch up and polish the code you write and be able to help those who respond to your query so that they provide to you a suitable approach in line with your skills for you to build upon. You will also get to help others who are coming after you up the path to monkeyness by posting questions that will stand the test of time because they're wholesome, clear and concise. So when you say
    'I'm not able to convert'
    you're being unclear for you've not mentioned what you have tried to do so far to accomplish this task.

    The above list is not exhaustive....Also take time to read:

    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: converting rows into column
by nvivek (Vicar) on Jul 19, 2010 at 06:04 UTC

    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 }
Re: converting rows into column
by suhailck (Friar) on Jul 18, 2010 at 16:21 UTC
    perl -MData::Dumper -e '%hash;while(<>) { ($hkey,$hval)=split /\s*:\s+ +/; push @{$hash{$hkey}}, $hval; };print Dumper(\%hash)' fileName


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-08-10 23:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found