Solved

PERL output a column into 2 columns

Posted on 2009-04-08
10
752 Views
Last Modified: 2012-06-27
I have a column that looks like this in a file .txt:
book1
book2
book3
book4
My PERL script reads this list into anto an array. So I assume that each element in this array is one line but I'd like to print this list into 2 columns.
book1 book2
book3 book4
Does anybody know how to do this? THANKSSS!!!
0
Comment
Question by:cucugirl
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24100788
while( <> ){
  s/\n/ / if $.%2;
  print;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 24100830
# if it's in @array=('book1'..'book4')
print $array[$_],(" ","\n")[$_] for 0..$#array;
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 24106051
Check this code.
This displays the entire contents of the array in two columns.

#!/usr/bin/perl
 
my @arr = ('a','b','c','d','e','f','g','h','i','j');
$col = 0;
 
for $val (@arr)
{
        print $val." ";
        $col ++;
        if ( $col % 2 eq 0) {
                print "\n";
        }
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cucugirl
ID: 24108365
the last solution will print out something like this:
book1
         book2
book3
         book4

same with the others.. I tried reading the file into an array and then separating this array into two loops an outer for rows and an inner for columns and print that but it still doesnt work :(
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24108510
If you are getting output like you posted, then it looks like the values in your array contain whitespace and/or newline characters.  Try this:
s/^\s*(.*?)\s*$/$1/s for (@array);
print $array[$_],(" ","\n")[$_ % 2] for 0..$#array;

Open in new window

0
 
LVL 5

Accepted Solution

by:
vikaskhoria earned 250 total points
ID: 24108680
Oh, that simply because each line which you read from a file has the new line character at the last, which is causing the array element to print in separate lines.
Just use the CHOMP function to get remove it, like this:

chomp($line);

So the new script is:
#!/usr/bin/perl
 
my @arr = ('a','b','c','d','e','f','g','h','i','j');
$col = 0;
 
for $val (@arr) {
        chomp($val);
        print $val." ";
        $col ++;
        if ( $col % 2 eq 0) {
                print "\n";
        }
}

Open in new window

0
 

Author Comment

by:cucugirl
ID: 24108855
awesoooome! this works! one more thing though... my columns look like this:

book1 book2
book3 book4
book5 book6
BUT since the title of each book has a different length.. is it possible to align them perfectly?
thanks guys! :D :D
0
 

Author Comment

by:cucugirl
ID: 24108863
i just did actually...

s/^\s*(.*?)\s*$/$1/s for (@array);
print $array[$_],(" ","\n")[$_ % 2] for 0..$#array;

replace with "\ t" rather than" " on the second line

THANKS! :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 24108994
#If you are modifying @array
s/^\s+//,s/\s+$// @aray;
#is more efficient
#without modifying @array, you could
printf (("%-40s","%s\n")[$_ % 2],$array[$_]=~/^\s*(.*?)\s*$/) for 0..$#array;

0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 24113705
Okay in case they are not of equal lengths, then yes what you did is good.
Append "\t" instead of " " at the print statement.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PERL get the value for query 4 157
perl to convert excel to csv 3 300
Linux rename files by reordering dates in the file name 6 39
Perl script to delete older files 6 94
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question