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.
In reply to Re: efficiently printing to 30 files
by BrowserUk
in thread efficiently printing to 30 files
by tbone
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |