Whenever I see variables with names delineated by sequential numbers, I tend think "Data structure". Sometimes a hash, usually an array. In this case, you not only had the Groupnarrays, but the Gn_Hashes and the Gn_out arrays. These can all be grouped into a single data structure which makes for easy looping.

The result is an AoH+A.

This code

#! perl -sw use strict; #! Define and populate an array of hashes my @Groups = ({}, {}, {}, {}, {}, {}); #! This would be easily done in a loop from another file. #! Populated each of the hashes with #! a Key ('array') set to an anonymous array #! Plus a key for each prefix in the group @{$Groups[1]}{'array', qw( A1 A2 A3 A4 A5 A6 A7 A8 A9 A0 )} = []; @{$Groups[2]}{'array', qw( B1 B2 B3 B4 B5 B6 B7 B8 B9 B0 )} = []; @{$Groups[3]}{'array', qw( C1 C2 C3 C4 C5 C6 C7 C8 C9 C0 )} = []; @{$Groups[4]}{'array', qw( D1 D2 D3 D4 D5 D6 D7 D8 D9 D0 )} = []; @{$Groups[5]}{'array', qw( E1 E2 E3 E4 E5 E6 E7 E8 E9 E0 )} = []; @{$Groups[6]}{'array', qw( F1 F2 F3 F4 F5 F6 F7 F8 F9 F0 )} = []; while (my $input = <DATA>) { chomp ($input); my $prefix = substr($input,0,2); for (1..6) { #loop over the AoH #! Push the value onto the groups array push @{$Groups[$_]{array}}, $input #! and skip the rest and last #! if the prefix exists in this groups hash if exists $Groups[$_]{$prefix}; } } { local $"=$/; #!" Print them out print "Group $_\n@{$Groups[$_]{array}}\n" for 1..6; } __DATA__ F3xxxxxxxxxxxxx15 A1xxxxxxxxxxxxx42 E5xxxxxxxxxxxxx47 A1xxxxxxxxxxxxx15 A7xxxxxxxxxxxxx70 A0xxxxxxxxxxxxx68 E7xxxxxxxxxxxxx34 C0xxxxxxxxxxxxx6 A9xxxxxxxxxxxxx24 F2xxxxxxxxxxxxx82 B2xxxxxxxxxxxxx46 E3xxxxxxxxxxxxx0 B8xxxxxxxxxxxxx40 D4xxxxxxxxxxxxx22 F4xxxxxxxxxxxxx33 B6xxxxxxxxxxxxx16 C5xxxxxxxxxxxxx9 D9xxxxxxxxxxxxx83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58 E8xxxxxxxxxxxxx5 Group 6 F3xxxxxxxxxxxxx15 F2xxxxxxxxxxxxx82 F4xxxxxxxxxxxxx33 C:\test>

You'll need to add code to handle prefixes that aren't in any group if that is a possibility.


Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

In reply to Re: Efficient Grouping by BrowserUk
in thread Efficient Grouping by meetraz

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.