Replacing an element in an array


I have a cgi script that takes in several values as parameters and reads a text file into an array.  It then greps the array for each value that has been passed in to see if that value already exists in the array (and by association the text file).  What I would like to do is that if a given value already exists in the array to replace that element with the newer one (that has been passed in) so that at the end of the script I can rewrite the array to the text file, and that file will have all the newest values.

The easiest way I can think to do this would be that if I could get grep to return the element ID in the array I could simply set the @array[$ID] = to the new value.  But I can't seem to find out how to get it to return the element ID while parsing the array.  Any help would be greatly appreciated.  Thanks.

PS.  The format of the data is a combination of alphanumeric characters, followed by -- one to four numbers -- another one to four numbers.  For example: mpd11--190--91.  I'm only matching the first set and it's the two sets of numbers I'm interested in updating.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, this looks like a job for a hash (associative array).  Basically, you want to read all your elements into a hash with the key being set to the first set (that you're matching) and the value being the line.

So if your data is already in the array and you want to put it into the hash:

my %hash;

foreach my $val (@array) {
       if ($val =~ /^(.*)\-\-(\d*)\-\-(\d*)$/) {
            $hash{$2} = $val;
       else {
            print "Warning: bad line!\n";

# Now for your new data just use:
$new_line = 'mpd11--190--91';

if ($new_line =~ /^(.*)\-\-(\d*)\-\-(\d*)$/) {
       $hash{$1} = $new_line;
else {
       print "Warning: bad new_line!\n";

# Two points:
# a) this has a side effect that if the new data isn't
#    in the file it *adds* it silently w/o warning.  If
#    you don't like that it's easy to change
# b) I didn't check the reg-ex's carefully against your
#    data, but if they don't work let me know (or just
#    use the ones you're using now for that matter!)

# hth,
# tats
okay, you are trying to search and replace in a file?

open INPUT "input.file";
open OUTPUT "output.file";

print OUTPUT $_;

close INPUT; close OUTPUT;
Hooray for the hash.
Use a hash.
You can check if the element in the hash exists before hand if you want by using
if (exists $hash{$key})
  # decide whether to add it:
  $hash{$key} = $newvalue;

  #or ignore it:
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

for( @array ){ s/value/newer/; }
_Dan_Author Commented:
Hey guys, thanks for the help, but I was able to do what I was looking for using grep and arrays (hashes sound interesting, but I'm not really familiar with them).  Here's the code I used:

@results = grep { $anotherarray[$_] =~ /@myArray[0]/ } (0..$#anotherarray);
foreach $result (@results)
  @anotherarray[$result] = @array[$i];

#@anotherarray has all of the old values
#@array has any new values to be added
#@myArray has the search parameters
$i = 0;
for( @anotherarray ){
    $_ = $array[$i] if /$myarray[0]/;
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.