?
Solved

PERL output a column into 2 columns

Posted on 2009-04-08
10
Medium Priority
?
762 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 85

Expert Comment

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

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 1000 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 85

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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

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 …
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
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…
Six Sigma Control Plans
Suggested Courses

589 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