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!

Array help. 1

Status
Not open for further replies.

FedoEx

Technical User
Oct 7, 2008
49
US
I have four columns input data.x,y,z b,c,d are some numbers not necessarily the same.
Code:
x 0 z1 bl
x 0 z2 cl
x 0 z3 dl
0 0 z1 bc
0 0 z2 cc
0 0 z3 dc
0 y z1 br
0 y z2 br
0 y z3 br
The data could be separated in blocks of triplets for which the third column is the same.
The sequence z1,z2,z3 would show three times in that order.




What is important is that the data always follows this patern.
There are no blank lines in the data. I put the blank line here for clarification. Here is actual test data.
Code:
4	0	 0.04	0.06 
6	0	 0.05	0.05
2	0	 0.06	0.04
0	0	 0.04	0.97
0	0	 0.05	0.96
0	0	 0.06	0.95
0	3	 0.04	0.01
0	6	 0.05	0.02
0	1	 0.06	0.03

7	0	 0.07	0.03
2	0	 0.08	0.04
5	0	 0.09	0.05
0	0	 0.07	0.94
0	0	 0.08	0.93
0	0	 0.09	0.02
0	3	 0.07	0.03
0	6	 0.08	0.02
0	1	 0.09	0.01
There are three cases for the first two columns.
Both zero. Only one of the $1 or $2 non zero.
The third column is always nonzero.

The operation I would like to perform on each triplet of data goes like that.
Get the third column values and subtract the middle fourth column from its two neighbors.
The first block with zeros for $1 and $2 would subtract fourth column from the previous and the following block.

0.04 0.97-0.06-0.01
0.05 0.96-0.05-0.02
0.06 0.95-0.04-0.03

The second triplet of data would be processed in a similar way:

0.07 0.94-0.03-0.03
0.08 0.93-0.04-0.02
0.09 0.92-0.05-0.01
Desired output is:
Code:
0.04	0.90
0.05	0.89
0.06	0.88

0.07   0.88
0.08   0.87
0.09   0.86
My approach to this is the following.
Define 2D array.
The index of this array gets zeroed every time when I go from one block of triplets to another.The third data column would be the second index of this array.
My code so far.

Code:
BEGIN{l=0;r=0;c=0}
$1==0&&$2==0{c=1; } #In these two brackets I update the array
$1==0&&$2!=0{r=1; } #
$1!=0&&$2==0{l=1; if (r!=0 ) print "New Block begins at "$3 ;r=0 }
So In the first two {....} I update the array
In the third {...} I update the array and print it because the test for the beginning of new block evaluates to true.

I hope that make sense.
Thanks.
 
A starting point:
Code:
NR%9==0{b[9]=$4;for(i=4;i<=6;++i)print z[i],b[i]-b[i-3]-b[i+3]}
{z[NR%9]=$3;b[NR%9]=$4}

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks.
I could use that approach.
The blocks are not necessarily 9 lines. It could change from one block to another. My bad I did not specify that. Still I believe I can use your approach . Will post back as soon as I get some solution.
 
Got it.
Code:
$1!=0&&$2==0{ b[$3]-=$4;} 
$1==0&&$2==0{ b[$3]+=$4;} 
$1==0&&$2!=0{ b[$3]-=$4;print $3, b[$3] }
It works for this particular case.
I am still looking for alternative solutions.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top