Solved

PERL output a column into 2 columns

Posted on 2009-04-08
10
747 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now