Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

phantom move between arrays

Status
Not open for further replies.

daddymack

MIS
Nov 13, 2001
15
IE
this has me flumoxxed, I set up two arrays, call and dat.
there is one exclusive element in both, and 4 inclusive, but when I delete from one of the arrays, the exclusive element magically moves to the other array. Is this a bug in awk ?

BEGIN {
# call[1] is exlusive to call array
call[1]=10
call[2]=20
call[3]=30
call[4]=60
call[5]=70
dat[2]=20
dat[3]=30
dat[4]=60
dat[5]=70
#dat6 is exclusive to dat array
dat[6]=80
print "elements in dat array"
for (i in dat)
print i,dat

print "where the values don't match"
for (i in call) {
if (call==dat) {
delete call
delete dat
print "got match , deleting element " i
}
}

print "in call array not in dat array"
for (i in call)
{
print i, call
delete call
print "deleted element from call array" i
}
print "in dat array not in call array"
for (i in dat)
print i,dat
}

OUTPUT is

elements in dat array
2 20
3 30
4 60
5 70
6 80
where the values don't match
got match , deleting element 2
got match , deleting element 3
got match , deleting element 4
got match , deleting element 5
in call array not in dat array
1 10
deleted element from call array1
in dat array not in call array
6 80
1

The problem lies with the one at the end(it looks like the element has moved from one array to another, but without the value). If I explicitly delete dat[1], it works fine, but I shouldn't have to..

please help
 
Sorry but the code in the above post interpreted "[ i ]" as italics and hence displayed incorrectly.. have replace with [j] and resubmitted

BEGIN {
call[1]=10
call[2]=20
call[3]=30
call[4]=60
call[5]=70
dat[2]=20
dat[3]=30
dat[4]=60
dat[5]=70
dat[6]=80
print "elements in dat array"
for (j in dat)
print j,dat[j]

print "where the values don't match"
for (j in call) {
if (call[j]==dat[j]) {
delete call[j]
delete dat[j]
print "got match , deleting element " j
}
}

print "in call array not in dat array"
for (j in call)
{
print j, call[j]
delete call[j]
print "deleted element from call array " j
}
print "in dat array not in call array"
for (j in dat)
print j,dat[j]
}
 
Sorry for the confusion but I think I know the reason now, apparently if you even reference an array element,it will create it with a null value. You have to check it first.

If you refer to an array element that has no recorded value, the
value of the reference is `""', the null string. This includes elements
to which you have not assigned any value, and elements that have been
deleted (see The `delete' Statement: Delete.). Such a reference
automatically creates that array element, with the null string as its
value. (In some cases, this is unfortunate, because it might waste
memory inside `awk').

You can find out if an element exists in an array at a certain index
with the expression:

INDEX in ARRAY

This expression tests whether or not the particular index exists,
without the side effect of creating that element if it is not present.
The expression has the value 1 (true) if `ARRAY[INDEX]' exists, and 0
(false) if it does not exist.

For example, to test whether the array `frequencies' contains the
index `"2"', you could write this statement:

if ("2" in frequencies) print "Subscript \"2\" is present."

Note that this is *not* a test of whether or not the array
`frequencies' contains an element whose *value* is `"2"'. (There is no
way to do that except to scan all the elements.) Also, this *does not*
create `frequencies["2"]', while the following (incorrect) alternative
would do so:

if (frequencies["2"] != "") print "Subscript \"2\" is present."


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top