bobdabuilda has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys. Been "hovering" around the forums for a while now, trying to pick up additional knowledge over time. Trying to pick up bits and pieces, and have been giving myself little mini-projects to try and stretch my knowledge and push the boundaries, etc.
To that end, I have a question on something which has me stumped. I've been Googling on and off for a couple of days, and looking over previous posts and examples etc. here on PM, but haven't found anything which (as far as I can tell) directly relates to what I want/need - there's every chance I've missed something simple, in which case I'll quite happily be told "where to go" :)
So - what I have, is some code which is pulling data out of a system, and pushing one of the values from that data into an array :
my @UACS; open(POLICY, "getpol -t UACS |"); while ($pol = <POLICY>) { chomp($pol); ($type, $number, $name) = split (/\|/, $pol, -1); push(@UACS,$name); # print "$name\n"; }
The commented print command is what I *was* doing - dumping the data in a single column to the screen. Effective, but cumbersome, when it's a number of pages of values.
What I would like to do is to output that list of values from the array in a columned format. The values would be anything up to around 20 characters in length (at a guess), and *may* contain spaces.
I've had a look at using the format command, but couldn't think how to go about parsing individual components of the array to different parts of STDOUT. One thing I tried was :
for my $row (@UACS) { format STDOUT = @<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<< @<<<< +<<<<<<<< shift(@row) . write; }
But I ended up with the error "Use of uninitialized value in hash element"
Hmmm... I just now thought about the possibility of going back and adding another loop, and going back to the print option, and adding in a tab character between each value for 3 or 4 loops, and then a \n. Sounds like it would work... but doesn't sound very... elegant.
Any suggestions, or even better - examples, would be very much appreciated. Thanks in advance - and also my apologies if I've missed something ridiculously easy :)
EDIT: One thing I forgot to mention, is that there's a fair chance this code, once finished, will be shared with others within my community around the world - all of whom are on different systems, versions, etc. so if able to do this without too many additional inclusions (for compatibility reasons), it would be preferable.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Output an array of values in column format?
by trizen (Hermit) on Mar 28, 2012 at 06:10 UTC | |
by bobdabuilda (Beadle) on Mar 28, 2012 at 06:29 UTC | |
|
Re: Output an array of values in column format?
by kcott (Archbishop) on Mar 28, 2012 at 06:49 UTC | |
|
Re: Output an array of values in column format?
by GrandFather (Saint) on Mar 28, 2012 at 08:15 UTC |