I would recommend reading the file in once. Then updating the array by inserting each new entry in it's proper place with splice. When finished inserting all the new entries, write the file out once.
my @reg_addrs = qw(bob@domain1.com bob@domain2.com); open FH,"<",$datafile or die "Could not read virtual file"; my @virtual = <FH>; close FH; for my $reg_addr (@reg_addrs) { my ($name,$domain) = split /@/,$reg_addr; my ($i) = 0; for my $entry (@virtual) { ++$i; if ($entry =~ /\Q$domain\E/) { splice @virtual,$i,0,"$reg_addr\t$user\n"; last; } } } open FH,">",$datafile or die "Could not write virtual file"; print FH @virtual; close FH;
| 90% of every Perl application is already written. ⇒ |
| dragonchild |
In reply to Re: running same bit of code twice
by pfaut
in thread running same bit of code twice
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |