Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: converting rows into column

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


in reply to Re: converting rows into column
in thread converting rows into column

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: converting rows into column
by shmem (Chancellor) on Jul 18, 2010 at 15:01 UTC

    I downvoted your post since you obviously did not read the links biohisham gave you.

    Had you done so, you would surely have formatted your second post with <p></p> and <code></code> tags:

    thanx so much for ur advice... actually am a new in perl monk...
    actually i want to make a database of mp3 songs. so after retrieving the tag info of each song..i got a output like :

    Artist: Rock Title: Fusion Album: Fusion Year: 2002 Genre: Other Artist: MelB Title: who am i: Album: donny Year: 2004 Genre: Other Artist: bon jo Title: i can stay Album : Armyman Year: 2009 Genre: Other Artist: Pearl Title: Last Album: Jam Year: 1983 Genre: Other

    now i want my database to be like :

    Artist Title Album Year Genre ------ ----- ----- ----- ----- Rock bbb hhh 2000 POP MelB Hum por 2003 other bon jo Armyman 209 others

    etc.

    #!/usr/bin/perl use warnings; use strict; my %hasharray; open (my $fh, "<", "C:/songdata1.txt") or die "can't open the file"; while (<$fh>){ my ($header,$info) = split /:/; }

    I dont know how to proceed futher.
    plz help me out.

    Now, to help you out:

    • make sure your records are separated with an empty line
    • read the input file in paragraph mode (switch -p00, see perlrun)
    • split the resulting records at "\n"(newline) into an array
    • iterate over this array, split each element into ($header,$info)
    • populate a hash (see perldata) with $header as key, $info as value
    • done iterating over the array, output the records (see perlform for formatted text output)

    Code (untested!) :

    #!/usr/bin/perl -p00 use warnings; use strict; open (my $fh, "<", "C:/songdata1.txt") or die "can't open the file: $!\n"; # $! gives a clue my %hash; while (<$fh>){ chomp; my @array = split /\n/, $_; for (@array) { my ($header,$info) = split /\s*:\s*/; # eliminate blanks befor +e/after ':' $hash{$header} = $info; } write; } format TOP = Artist Title Album Year Genre ------ ----- ----- ---- ----- . format STDOUT = @<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<< @<<< @<<<<<<<<<<< +<< $hash{Artist}, $hash{Title}, $hash{Album}, $hash{Year}, $hash +{Genre} .

    Instead of repeating $hash{} in format STDOUT, you could also use a hash slice:

    @hash{ qw(Artist Title Album Year Genre) }
Re^3: converting rows into column
by ww (Archbishop) on Jul 18, 2010 at 14:34 UTC
Re^3: converting rows into column
by changma_ha (Sexton) on Jul 18, 2010 at 15:06 UTC

    thanx so much for ur advice... actually am a new in perl monk... actually i want to make a database of mp3 songs. so after retrieving the tag info of each song..i got a output like :

    Artist: Rock Title: Fusion Album: Fusion Year: 2002 Genre: Other Artist: MelB Title: who am i Album: donny Year: 2004 Genre: Other Artist: bon jo Title: i can stay Album : Armyman Year: 2009 Genre: Other Artist: Pearl Title: Last Album: Jam Year: 1983 Genre: Other
    now i want my database to be like :
    Artist Title Album Year Genre ------ ----- ----- ----- ----- Rock Fusion Fusion 2002 other MelB who am I dont 2003 other bon jo Armyman jojo 2009 others etc. #!/usr/bin/perl use warnings; use strict; my %hasharray; open (my $fh, "<", "C:/songdata1.txt") or die "can't op +en the file"; while (<$fh>){ my ($header,$info) = split /:/; }
    I dont know how to proceed futher. plz help me out. sorry for making mistake earlier. now i got it rite.. sorry biohisam..

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2022-10-04 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (19 votes). Check out past polls.

    Notices?