in reply to running same bit of code twice
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 |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: running same bit of code twice
by jonnyfolk (Vicar) on May 04, 2003 at 18:30 UTC | |
by belg4mit (Prior) on May 04, 2003 at 18:43 UTC | |
|
Re: Re: running same bit of code twice
by Anonymous Monk on May 04, 2003 at 17:57 UTC | |
by The Mad Hatter (Priest) on May 04, 2003 at 18:09 UTC | |
by Anonymous Monk on May 04, 2003 at 18:13 UTC |