Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

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.
1 Solution
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 **

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now