Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PERL output a column into 2 columns

Posted on 2009-04-08
10
Medium Priority
?
761 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

877 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