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

Reading a file into 2x dimensional array; can't read in body

Status
Not open for further replies.

chilecayenne

Programmer
Aug 16, 2011
4
US
Hi all,

I'm trying my hand at awk again, but just can't seem to get things going. I'm starting this to try to load one file into a 2x dimensional array, and in the body, search it to match on fields of the file I'm processing and append some values for output.

This is in the early stages...just trying to access the multi-dimensional array in the body.
It appears to be getting loaded in the BEGIN section...print statements show the values, and even the score from the array with the values for memrecnos in it.

BEGIN {
FS="|";
OFS="|";

while (getline < "test_link.txt" > 0)
{
split ($0,bxm_rec,"|");

b_memrecno1 = bxm_rec[2];
b_memrecno2 = bxm_rec[3];
b_score = bxm_rec[5];


bxm_array[b_memrecno1,b_memrecno2] = b_score;



}

}

#start main block
{

v_memrecno1 = $2
v_memrecno2 = $8

print "Memrecno1 = "$2" Memrecno2 = "$8"\n"

printf ("Score = %s\n",bxm_array[v_memrecno1,v_memrecno2])

#printf ("Score = %s\n",bxm_array[11,113521])




} #end of main block


However, when feeding it values from the input file (lets call it file2)...I get the following output:

Memrecno1 = 11 Memrecno2 = 113521

Score =
Memrecno1 = 29622 Memrecno2 = 46523

Score =
Memrecno1 = 33710 Memrecno2 = 45357

Score =
Memrecno1 = 36596 Memrecno2 = 47977

Score =
Memrecno1 = 36614 Memrecno2 = 47961

Score =
Memrecno1 = 37127 Memrecno2 = 37142

Score =
Memrecno1 = 37141 Memrecno2 = 45462

Score =
Memrecno1 = 37465 Memrecno2 = 56467

Score =
Memrecno1 = 37519 Memrecno2 = 46299

Score =
Memrecno1 = 37537 Memrecno2 = 47179

Score =


As you can see I've tried in one print statement (currently commented out) to hardcode values that I KNOW are in there, and have shown while using print statements in the array load section that they are going in there....


Any suggestions?

TIA,

chilecayenne


 
Without seeing your input data it's a bit difficult to answer.

A few comments on the code though; no need for split when you have defined the FS, even with getline awk will split the fields for you automatically.

Also, why assign things to variables unnecessarily. The existing code can be reduced to:

Code:
[green]BEGIN[/green] {
    [blue]FS[/blue]=[red]"[/red][purple]|[/purple][red]"[/red];

    [olive]while[/olive] ([b]getline[/b] < [red]"[/red][purple]test_link.txt[/purple][red]"[/red] > 0) { bxm_array[[blue]$2[/blue],[blue]$3[/blue]] = [blue]$5[/blue]; }
}

[gray]#start main block[/gray]
{
    [b]print[/b] [red]"[/red][purple]Memrecno1 = [/purple][red]"[/red][blue]$2[/blue][red]"[/red][purple]   Memrecno2 = [/purple][red]"[/red][blue]$8[/blue][red]"[/red][purple]\n[/purple][red]"[/red]

    [b]printf[/b] ([red]"[/red][purple]Score = %s\n[/purple][red]"[/red],bxm_array[[blue]$2[/blue],[blue]$8[/blue]])

    [gray]#printf ("Score = %s\n",bxm_array[11,113521])[/gray]

} [gray]#end of main block[/gray]


Annihilannic.
 
Code:
Without seeing your input data it's a bit difficult to answer.

A few comments on the code though; no need for split when you have defined the FS, even with getline awk will split the fields for you automatically.

Also, why assign things to variables unnecessarily.  The existing code can be reduced to:

CODE --> awk
BEGIN {
    FS="|";

    while (getline < "test_link.txt" > 0) { bxm_array[$2,$3] = $5; }
}

#start main block
{
    print "Memrecno1 = "$2"   Memrecno2 = "$8"\n"

    printf ("Score = %s\n",bxm_array[$2,$8])

    #printf ("Score = %s\n",bxm_array[11,113521])

} #end of main block

Thank you for the reply.
Well, I've not done awk in quite awhile...and also, trying something new putting one file into an array to use to grab values for the file coming in to process.

I figured I'd be verbose at first using all the variables I needed, etc....I can always clean up at the end.

Ok, an example line from the test_link.txt file I'm reading into the array would be:

|64397|170202|-9223140293085304746|236|

The file I'm reading in, file2.txt would be something like this:

11|64397|IHOPV-200-NI-1001131054V336447|IHOP|NI|200|1001130004V336447|170202|IHOPV-200-NI-1008588474V760984|IHOP|NI|200|1110588474V760984|1001131999V336447|1008588474V760984

I'm eventually in the code going to be having fields $2 and $3 from the first line matching fields $2 and $8 in the second line to do some processing.

I can't, however, at this point in time...get anything to print out of my two dimensional array.

Is this somehow a case of a scope problem? That setting the array in the BEGIN section, won't allow it to be accessed in the BODY section? I didn't think awk had that kind of problems with scope, but wondering if this might be one?

Thank you in advance,

chilecayenne




 
Ok...I think I solved this one.

Not exactly sure what happened...the hard coding of the values in the array read never worked...BUT, when I put in some real values that matched in the bxm_array[$2,$8] section...it started returning meaningful values.


So, SOLVED on this one..

Thank you,

CC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top