in reply to Re: Re: Inserting lines in a file with Tie::File if lines don't exist
in thread Inserting lines in a file with Tie::File if lines don't exist
It is fine to stay with array. Hope the following code helps:
Made a general purpose function to handle both replace and insert, and determines whether to insert or replace on its own:
use warnings; use strict; my @a = ("a=1", "b=2", "c=3"); print join(",", @a), "\n"; add("b", 22);#this replaces print join(",", @a), "\n"; add("d", 45);#this inserts print join(",", @a), "\n"; sub add { my ($key, $val) = @_; my $exists = 0; for (grep /^$key=/, @a) { $_ = "$key=$val"; $exists = 1; } if (!$exists) { push @a, "$key=$val" } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Inserting lines in a file with Tie::File if lines don't exist
by devgoddess (Acolyte) on Dec 31, 2003 at 01:54 UTC | |
|
Re: Re: Re: Re: Inserting lines in a file with Tie::File if lines don't exist
by bean (Monk) on Dec 31, 2003 at 20:34 UTC |