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.


In reply to Output an array of values in column format? by bobdabuilda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.