I'm ducking a %hash or a db, even though the question plainly implies use of one or the other, for what may be (in this particular case) a semi-good reason, and I'm curious about better ways to handle to particular data.

Given the two arrays (below), each comprised of multiple sets of elements, and which share, db-like, a first element in each set*1, is this code demonstrating yet another of my blind spots or an unnecessarily clumsy way to output related sets?

*1 The the comments after each array assignment may explain this more clearly.
#!/usr/bin/perl use strict; use warnings; my @arr = ("1", "Smith", "Ric", "1", "Smith", "Rich", "1", "abc", "A", + "1", "cdeQ", "C Sr", "1", "ghi", "G", "2", "cdeQ", "C Sr", "3", "ghi +", "G", "3", "abc", "A", "3", "Smith", "Ric", "4", "cde", "C", "5", " +cde", "C", "5", "xyz123", "X", "5", "cdeQ", "C II", "5", "Smith", "Ri +ch"); # callno lname fi (repeat 3 data points for each FF at each + callno) my @arr2 = ("1", "S", "01/05", "04", "2", "EMS", "01/05", "07", "3", " +VF", "01/05", "11", "4", "S", "01/05", "13", "5", "B", "01/05", "17") +; # callno type date hr (repeat 4 data points for each in +cident) my ($i, $j, $seen); $i = 0; $j = 0; for my $arr2(@arr2) { if ( $arr2[$j+3] ) { # ie, if there's a complete call r +ecord print "$arr2[$j] " . "$arr2[$j+1] " . "$arr2[$j+2]" . "$arr2[$ +j+3]\n"; $seen = substr($arr2[$j], 0); print "\tDEBUG: My \$seen: $seen\n\n"; $j += 4; } else { print "\n\t\t No more calls\n"; exit; } for my $arr(@arr) { if ( ($arr[$i]) && ($seen == substr($arr[$i], 0)) ) { print "$arr[$i] " . "$arr[$i+1] " . "$arr[$i+2] " . "\n"; $i += 3; next; } else { print "\n\t\tFF Array exhausted for Callno $seen\n"; last; } } next; }

This "works" in that the output is in a form the users can live with:

1 S 01/0504 DEBUG: My $seen: 1 1 Smith Ric 1 Smith Rich 1 abc A 1 cdeQ C Sr 1 ghi G FF Array exhausted for Callno 1 2 EMS 01/0507 DEBUG: My $seen: 2 2 cdeQ C Sr FF Array exhausted for Callno 2 3 VF 01/0511 DEBUG: My $seen: 3 3 ghi G 3 abc A 3 Smith Ric FF Array exhausted for Callno 3 4 S 01/0513 DEBUG: My $seen: 4 4 cde C FF Array exhausted for Callno 4 5 B 01/0517 DEBUG: My $seen: 5 5 cde C 5 xyz123 X 5 cdeQ C II 5 Smith Rich FF Array exhausted for Callno 5 No more calls

Suggestions of YAM (™ "yet another method") would also be welcome.


In reply to Iterating two co-dependant arrays by ww

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.