Hi all, I just learned Perl for my work and I've got a quick question if someone could help me. A program I wrote reads in a list of eight-bit hex numbers. The first four is a command, the last four are arguments to that command. My program generates a summary of which commands occurred and what arguments did they occur with. Simple enough:
Command 01a3 executed 6 times: 0213 0214 0215 0216 0217 0218
The thing is, some commands will run around 35 to 40 times, and to keep them all in one column is not exactly a great use of space considering these reports are printed and kept hard-copy. So, what I need is a way to create multiple columns (up to 4) based on how many entries there are in the array in which I keep those numbers. I wrote a fast solution to do this with duplicate commands/arguments as the entries, but it's not the most elegant (I begin teaching myself Perl about a month ago):
###this code only prints up to 3 columns, not 4 print OUT "\nThe following ", scalar @dupes, " tokens were duplicated: +\n"; if(@dupes<=10) { foreach (@dupes) { print OUT "\t$_\n"; } } elsif(@dupes<=20) #print two columns of dupes { for($i=0; $i<=10; $i++) { if(defined ($dupes[10+$i])) { $_=$dupes[$i]."\t".$dupes[10+$i]; } else { $_=$dupes[$i]; } print OUT "$_\n"; } } else #print three columns { no warnings; for($i=0; $i<=(@dupes/3); $i++) { if(defined $dupes[$i+(2/3)*@dupes]) { $_=$dupes[$i]."\t\t".$dupes[(1/3)*@dupes+$i+1]."\t\t".$dup +es[(2/3)*@dupes+$i+2]; } elsif(defined $dupes[$i+(1/3)*@dupes]) { $_=$dupes[$i]."\t\t".$dupes[(1/3)*@dupes+$i+1]; } else { $_=$dupes[$i]; } print OUT "\t$_\n"; } }
While I can probably use this code to implement multiple columns, I would probably screw something up when it came time to maintain it. It took forever to tweak that code as it was to make sure I didn't either accidentally reprint a line or skip a line. Any ideas? I'd be much obliged.

In reply to Long array -> multiple columns? by azredwing

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.