acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense?

/pattern/ {tst[$2]=$3}
(( getline < "file" ) > 0 ) {
        x[$2]=x[$2] " "tst[$2]
}

When I print tst[$2] in the END statement it is assigned, but when I try to print tst[$2] in the while statement with getline is shows unasigned. How do I reference tst[$2] while reading in file with getline???

Thanks in advance.

I suppose FS has been modified,
try to force the split (assuming default OFS value, otherwise use whatever you need):

/pattern/ { tst[$2] = $3 }
(( getline < "file" ) > 0 ) {
        split($0, t, OFS)
        x[t[2]] = x[t[2]] FS tst[t[2]]
}

Thanks for the reply radoulov, still can't get this figured out. I think if I could reference it right, it will work. How come the following code prints out null, but if I use the same print statement in the END statement it will be successful

/pattern/ {tst[$2]=$3}
(( getline < "file" ) > 0 ) {
        print tst[23]   #will print null
}
END {
printf "%s--%s\n",tst[23],"END"  # is successful

Where is all the input coming from? Are you using only a file named "file" for all your input needs and can you post the entire awk script along with some input data?

In first part of awk script tst array is set for use in the getfile section of script. Problem I have got is, it looks like I can't use tst array while reading in and setting x array.

#!/bin/ksh
tput clear

dir=/my/dir/here/ #all files are gzip'd
ifile=/tmp/ifile
ofile=/tmp/ofile
$file=$dir$file.gz
echo "Enter the date and hour to run this script on [YYMMDDHH]: \c"
read file


#tool is run to generate $ofile here, file is semicolon delimited


/usr/bin/gzcat $file | /usr/xpg4/bin/awk '

BEGIN {
print "headers go here"
}


NR == 3 { print $2,$3"  --  "$4,$5 ; print "" ; next }

/^dise / {
        dipce = substr($2,4,1) * 1000 ; next
           }
/^LC/ {
        lec = substr($0,3,3) + dipce
        f++
        s = ""
        for ( i in arr ) { delete arr }
        }
	   f {
              if ( $1 == "RRAC" ) {
                s++
                 }
                if ( s ) {
                    if ( $1 == "tsec" ) {
                       lectsec = $2 * 1
                       }
                    if ( $1 == "VAR" ) {
                        car[$2 * 1]+=$8
                        lecCar = $2 * 1
                        c[lec,lectsec,BCar] = 0
                        }
                     c[lec,lectsec,BCar]++
                     if ( c[lec,lectsec,BCar] == 8 ) {
                        car[BCar]+=$8
                        tst[lec"-"BCar]=sprintf("%5.2f",car[BCar] / 12)
                        }
                    }
                if ( $1 == "RRAC-L" ) {
                        f = "" ; next
                   }
                   }


###This section not working because I can't access###
###array tst, to "test" I tried printing tst array, but can't get it to work.

{
while (( getline < "'"$ofile"'" ) > 0 ) {
FS = "\;" 
	x[$2"-"$4]+=tst[$5"-1"] ;
        x[$5"-"$7]+=tst[$5"-2"] ;
        x[$8"-"$10]+=tst[$5"-3"];
        x[$11"-"$13]+=tst[$5"-4] ;
}
}
#####################################################
END {
for ( i in x ) { print i,x }

    } '

/usr/bin/rm $ifile
/usr/bin/rm $ofile


This action is executed for every record (while, I suppose, your array in not yet build):

{
  while (( getline < "'"$ofile"'" ) > 0 ) {
  FS = "\;" 
    x[$2"-"$4]+=tst[$5"-1"] ;
        x[$5"-"$7]+=tst[$5"-2"] ;
        x[$8"-"$10]+=tst[$5"-3"];
        x[$11"-"$13]+=tst[$5"-4] ;
  }
}

Try moving it into the END block.

You can read both files in parallel if you need, the array infile is accessible consider this:

zsh-4.3.4% head *file
==> getlinefile <==
1 getlinefile
2 getlinefile

==> infile <==
1 infile
2 infile
zsh-4.3.4% awk '{ infile[$1] = $0 }
quote> (getline < "getlinefile") {
quote>   print infile[$1], $0
quote>   }' infile
1 infile 1 getlinefile
2 infile 2 getlinefile

This is not a good habit:

"'"$ofile"'"

Use this instead:

awk -v ofile="$ofile" ...

Thanks, I did not think of that. Getting closer but still not there. I moved to end statement like you sugested, but can you explain the following code:

(( getline < "file" ) > 0 ) {
        printf "%s--%s\n",tst["23"],"WHILE"     #this prints the element of tst[23]
	printf "%s\n",tst["\42"20+3"\42"]	#this print null???  why???

}

Because they are different:

zsh-4.3.4% awk 'BEGIN {
quote>   tst["23"]
quote>   tst["\42"20+3"\42"]
quote>   for (k in tst)
quote>     print k
quote> }'
23
"23"

If you want them equal:

zsh-4.3.4% awk 'BEGIN {
quote>   tst["\42"23"\42"]
quote>   tst["\42"20+3"\42"]
quote>   for (k in tst)
quote>     print k
quote> }'
"23"

And I suppose you need just:

zsh-4.3.4% awk 'BEGIN {
quote>   tst[23]
quote>   tst[20+3]
quote>   for (k in tst)
quote>     print k
quote> }'
23

Thanks ALOT I guess I got confused and frustrated. I was testing it wrong. I will try it on my script.

It's working! Can't thank you enough. No matter what I tried, I just couldn't get it going.

Thanks again.