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!

for loop doesn't execute

Status
Not open for further replies.

domenicodl

Programmer
Jul 2, 2011
15
IT
Hello,

I have two files, namely file1 and file2 as follows

file1:
000020E0
000020E4

file2:
000020E0 3D 20 00 40 lis r9,400000
000020E4 39 29 A0 00 addi r9,-6000
000020E8 80 09 00 00 lwz r0,00(r9)
000020EC 7C 03 03 78 mr r3,r0
000020F0 81 61 00 00 lwz r11,00(r1)

I want to take the lines in file1 compare them with the first field of each line of file2 and if they are equals do some processing on the line (of the file2)

for example, I take the first line in file1, 000020E0 I compare it with 000200E0 in the second file, they are equals thenn I take some fields from this line and I do some processing.

I wrote the following script:

BEGIN { print "PARSER"}
#open the first file.
NR==FNR{
pc[NR] = $1; #pc is the first field in file 1
number_of_pc = NR;
next;
}
#reading of the first file finished.
#Starting with the second one
{
pc_disass = $1;
instruction = $6;
register = $7;
#compare each line in file1 (the pc array) with
the first field of the current line in file2 (pc_dissass)
for( i = 1; i == number_of_pc; i++ )[
if ( pc_disass == pc)
print pc_disass;
}
next;

}
END {print "- DONE -"}

I do not know why the for loop is never executed albeit it is written correctly.
Thank you in advance.


 
This is what I do in windows:
call this with "-ffilename2.txt"
Code:
BEGIN {
      print "BEG TIME......... "strftime("%H:%M:%S") #I like to know the time
       //load array of fileone.txt this is ok for about 800,000 records on my pc anyway 
       while ( getline fileonerec < "c:/fileone.txt") # <- this is file one
       {recnt++ 
       pc[fileonerec]=fileonerec
       }
print recnt " Records in fileone.txt"
}
{
#todo
if ($1 in pc){
print pc[$1]  # which is the same as print $1 for this array
matcharr[$1]=$0 # maybe more useful create array of matches

}
}
END{
    for(x in matcharr){
    print x,matcharr}
    print "END TIME ........ " strftime("%H:%M:%S") #done in seconds
}
check for typos because I didn't but you get the idea
 
Change == to <= in your for statement

Code:
  for( i = 1; i [COLOR=red]<=[/color] number_of_pc; i++ ){

Or you could do it slightly differently
Code:
BEGIN { print "PARSER"}
NR==FNR{
  pc[$1] = 1
  next
}
#reading of the first file finished.
#Starting  with the second one
pc[$1]{
 pc_disass = $1
 instruction = $6
 register = $7
 print pc_disass
}
END {print "- DONE -"}

CaKiwi
 
Thank you very much, both.
I adopted the easiest solution, replacing == with <=.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top