Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 }

In reply to Re: converting rows into column by nvivek
in thread converting rows into column by changma_ha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-19 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found