Replacing an element in an array

Posted on 2003-03-07
Medium Priority
Last Modified: 2013-12-25

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.
Question by:_Dan_
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8090929
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

Expert Comment

ID: 8092914
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;

Expert Comment

ID: 8111279
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:
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

LVL 84

Expert Comment

ID: 8116948
for( @array ){ s/value/newer/; }

Author Comment

ID: 8129111
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
LVL 84

Expert Comment

ID: 8133141
$i = 0;
for( @anotherarray ){
    $_ = $array[$i] if /$myarray[0]/;

Accepted Solution

Mindphaser earned 0 total points
ID: 8427641
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question