Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Print array vertically (in columns)

by liverpole (Monsignor)
on Sep 02, 2009 at 00:09 UTC ( [id://792772]=note: print w/replies, xml ) Need Help??


in reply to Print array vertically (in columns)

An interesting solution to an interesting problem.

I noticed you didn't use warnings; putting them in gives this:

Use of uninitialized value in concatenation (.) or string at x.pl line + 47. Use of uninitialized value in concatenation (.) or string at x.pl line + 47. ...
But you can fix it as easily as changing the line:
$w_columns{$w_col_ix} = "$w_columns{$w_col_ix}${w_pad}${w_char}";

to:

$w_columns{$w_col_ix} .= "${w_pad}${w_char}";

Then, for fun, I tried golfing it.  Assuming the second parameter will almost always be spaces, I changed it from a string to the number of spaces (but left the default at 2).

Here's my result:

use strict; use warnings; my $a_rows = [qw[one two three four five six seven eight nine ten]]; my $npad = 4; my $justify = 1; rows_to_cols($a_rows, $npad, $justify); sub rows_to_cols { my ($x, $p, $j) = @_; my $m = 0; map { $m = length $_ if $m < length $_ } @$x; ($j||0) && map { $_ = sprintf "%*s", $m, $_ } @$x; @_ = map { ($")x($p||2),$_ } @$x; map { map { print substr($_, 0, 1, "") || $" } @_; print $/ } 1..$ +m }

And here's what's happening:

sub rows_to_cols { my ($x, $p, $j) = @_; my $m = 0; map { $m = length $_ if $m < length $_ } @$x; ## # Assigns to $m the maximum length of any word ($j||0) && map { $_ = sprintf "%*s", $m, $_ } @$x; ## # If bottom-justifying, converts every word to the # right-justified version of the same. Thus: # # [ [ # 'one', ' one', # 'two', ' two', # 'three', 'three', # 'four', ' four', # 'five', => becomes => ' five', # 'six', ' six', # 'seven', 'seven', # 'eight', 'eight', # 'nine', ' nine', # 'ten' ' ten', # ] ] @_ = map { ($")x($p||2),$_ } @$x; ## # Creates (in @_) vertical padding between each word. # For example: # [ # ' ', # ' ', # ' ', # ' ', # ' one', # ' ', # ' ', # ' ', # ' ', # ' two', # ' ', # ' ', # ' ', # ' ', # 'three', # ' ', # ' ', # ... # ' ', # ' ten' # ] # Note that the data is now in a horizontal format that, # if converted to the vertical equivalent, would be the # the desired result. map { map { print substr($_, 0, 1, "") || $" } @_; print $/ } 1..$ +m ## # This does the magic horizontal to vertical conversion }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: Print array vertically (in columns)
by perlofwisdom (Pilgrim) on Sep 03, 2009 at 16:16 UTC

    Brilliant! This is what I love about PerlMonks: people sharing, improving and (in my case) learning! Although I've been coding in perl for a while now, I sometimes get stuck in the rut of doing things the same way. And, then I look at someone else's solution to a similar issue, and WHAM! -- I never thought of doing it that way -- that's WAY better.

    So, thanks for taking the time to play around with my code. I had to stare at a couple of your modified statements awhile, to make sure I understood how you did it in so little code (your cheater block helped).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://792772]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found