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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question for vgersh99

Status
Not open for further replies.

ranjit

Technical User
Apr 14, 2000
131
GB
You recently posted scripts to carryout file comparisons. Why is the array "arrA" in the below BEGIN statement indexed by $1 i.e. arrA[$1]=$0, however...in the second example the array is indexed with an incrementer i.e. arr[++lineA]=$0

I know the scripts perform different tasks but I can't understand the logic behind the choice to
index the array - clearly it makes a difference.


=========================================
Subject: Comparing two files

nawk -v fn=fileA -f comm.awk fileB

#------------------- comm.awk
BEGIN {
if (!fn) fn = "fileA"
fileAprime= fn ".prime"
while((getline < fn) > 0)
arrA[$1] = $0
}

FNR == 1 { fileBprime= FILENAME &quot;.prime&quot; }

{
if ( !( $1 in arrA))
print $0 >> fileBprime;
else
delete arrA[$1];
}
END {
for (i in arrA)
print arrA >> fileAprime;
}

======================================

Subject: Can Two input files be processed at awk..?

BEGIN {
if (!fn) fn = &quot;fileA&quot;
lineA=0
while((getline < fn) > 0)
arr[++lineA] = $0
}

========================================


I would appreciate it if you could clear up the confusion.

Thanks
 
I just recently started dealing with arrays within awk and found out that all awk arrays are associative arrays, making it possible to use strings and numerics to index the array.

Advantage is that you can test for membership by using strings as index. In my opinion that is what was used in the 1st example. The reason being that you could test for membership in the array and act accordingly.

Drawback is sorting within the array and especially looping through parts of the array. Thats where you would rather use numeric indices.

e.g.

myWeek_array[Mon]
myWeek_array[Tue]
myWeek_array[Wed]
myWeek_array[Thu]
myWeek_array[Fri]
myWeek_array[Sat]
myWeek_array[Sun]

vs.

myWeek_array[1]
myWeek_array[2]
myWeek_array[3]
myWeek_array[4]
myWeek_array[5]
myWeek_array[6]
myWeek_array[7]

Try running through you array for all weekdays. In the 1st example you will have some difficulty (you will need to explicitly list all occurrences), but in the 2nd example you can write a loop from 1-5.

I hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top