There are a couple of ways you could possibly improve the efficiency of this process, but whether they are useful will depend on if I have read between the lines of your post correctly.

First, you give a definitive number of 30 files, which suggests to me that the /ABC/ and /DEF/ are placeholders for Department or Division names or codes, and it is possible that the value of $emp{n}{Org} is the entire thing? Ie. 'ABC' or 'DEF' from your example. Of course, if this is the case, then you would (should) be using eq not =~ for your comparison, so if I've reached a step too far, hit the -- and ignore the rest of this post.

Your still here? Phew! Okay, I am guessing that your hash looks something like

my %emp = ( 987654 => { Emp=>'A. Employee', Org=>'ABC', '...'=> }, 987653 => { Emp=>'A.N.Other', Org=>'DEF', '...'=> }, # ..... );

If this is anything like close to the real situation, you could probably save time by creating a (temporary?) HoA's that mapped Org=>Emp. Something like

my %OrgEmp; push @{ $OrgEmp{$_}{Org}}, $emp{$_}{Emp} for keys %emp;

Or if your world doesn't fit with my neat assumptions and your Org ids are more like 'ABC001' & 'DEF34', then you would still need to use regexes, but you could do it when building the temp HoA's

my $re_org = qr[ '(' . join('|', qw[ABC DEF ...]) . ')' ]o; my %OrgEmp; for my $empNo (keys %emp) { push @{ $OrgEmp{$1} }, $emp{$empNo}{Emp} if $emp{$empNo}{Org} =~ $ +re_org; }

In either case, the processes of writing out the names of the employees in each organisation becomes trivial

for my $org (keys %OrgEmp) { open OUT, '>', 'Group' . $org or die $!; print OUT "$_\n" for @{ $OrgEmp{$org} }; close OUT; }

However, if your data does allow either method and you still need to hold all 30 files open and write to them simultaneously, then you could at least reduce the need for all that repeated PRINT A .... if ....; by building another data structure to hold your filehandles and associate them with the regexes.

my %groups = map{ open my $fh, '>', "Group$_" or die $!; $_ => $fh; } qw[ABC DEF ...]; for my $empNo (keys %emp) { for my $grp (keys %groups) { #! Note the {} (bare block) around the filehandle are necessar +y. print { $groups{$grp} } $emp{$empNo}{Emp} if $emp{$empNo}{Org} + =~ m[$grp]; } }

This latter method won't save any processing time, but it could save you some typing and it is scalable when the number of Org's changes. Just add or delete them from the qw[...] on the map and the rest of the program still works.

As shown, the filenames would end up as GroupABC, GroupDEF, etc which may not fit with your needs, but it probably easier to rename them afterwards than to build the mapoing of regex to GroupA, GroupB, though even that's not so hard.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

In reply to Re: efficiently printing to 30 files by BrowserUk
in thread efficiently printing to 30 files by tbone

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.