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!

Find max and min by using array, get problem

Status
Not open for further replies.

will27

Technical User
Jun 13, 2007
23
US
Hi, all
The data is like following:
############## data ####################
time name price
20050616 company2 32
19900112 company1 39
19980223 company2 44
20060523 company1 55

############# job to be done ################
the job is simple, find the starting and ending time ofall companies and calculate their return, e.g., for company1, the return is 55/39-1, for company2, it is 32/44-1.

Following code print out nothing, can anybady tip me the error? your help appreciated!
By the way, suggestion on how to initialize an array with customized value (not null) also appreciated.
Regards
will

#################### code ###############################
awk '{BEGIN{FS=OFS="\t";time=1;tk=2;pr=5}
{ if($time > maxtime[$tk]) {maxtime[tk]=$time; etp[$tk]=$pr};
if (mintime[$tk]=="") {mintime[tk]=$time;stp[tk]=$pr};
if(mintime[$tk]!="" && $time < mintime[$tk]) {mintime[tk]=$time; stp[$tk]=$pr} }
END{ for (s in etp) {if (etp!="" && stp!="") print s, maxtime,etp,mintime,stp, etp/stp-1} }' zzz_tmp



 
Hi

You have an extra opening brace ( { ) in front of the code.

You have pr=5 and use $pr, but you have no 5[sup]th[/sup] field. ( If happens to have, then please give such explanation when posting data. Also please post your sample data with fixed width characters using [tt][ignore][tt][/ignore][/tt] [tt][ignore][/tt][/ignore][/tt] tags. By the way, please post code between [tt][ignore]
Code:
[/ignore][/tt] [tt][ignore]
[/ignore][/tt] tags. )

I would try it like this :
Code:
awk 'BEGIN{FS=OFS="\t"}NR==1{next}t[$2]{print $2,(t[$2]>$1?p[$2]/$3:$3/p[$2])-1;next}{t[$2]=$1;p[$2]=$3}' zzz_tmp
Tested with [tt]gawk[/tt].

Feherke.
 
Hi, feherke
Thank you for your tip and sorry for the confusion, I did some modification to fit the pseudo example when wrote this post but not careful enough.
your code is succinct and elegant, howover, I am still wondering what's the problem with my code as following.


Code:
awk 'BEGIN{FS=OFS="\t";time=1;tk=2;pr=3}

     { if($time > maxtime[$tk]) {maxtime[tk]=$time; etp[$tk]=$pr}; 
       if (mintime[$tk]=="")    {mintime[tk]=$time;stp[tk]=$pr}; 
       if(mintime[$tk]!="" && $time < mintime[$tk]) {mintime[tk]=$time; stp[$tk]=$pr}                                                }

     END{ for (s in etp) { print s, maxtime[s],etp[s],mintime[s],stp[s], etp[s]/stp[s]-1} }'  zzz_tmp

thank you
will
 
I am still wondering what's the problem with my code
WHICH PROBLEM ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

will said:
Following code print out nothing
I got the same result. Nothing.

At the first look you missed the dolar sign ( $ ). Change all [tt]tk[/tt] to [tt]$tk[/tt]. But that is not enough. I think you have to refine the second [tt]if[/tt]'s condition.

Sorry, but is abit difficult to me to follow the logic. That is why I prefered to suggest a new one. :-(

Feherke.
 
I am still wondering what's the problem with my code
Code:
awk 'BEGIN{FS=OFS="\t";time=1;tk=2;pr=3}
[!]NR>1[/!] { if($time > maxtime[$tk]) {maxtime[[!]$[/!]tk]=$time; etp[$tk]=$pr}; 
       if (mintime[$tk]=="")    {mintime[[!]$[/!]tk]=$time;stp[[!]$[/!]tk]=$pr}; 
       if(mintime[$tk]!="" && $time < mintime[$tk]) {mintime[[!]$[/!]tk]=$time; stp[$tk]=$pr}
     }
 END{ for (s in etp) { print s, maxtime[s],etp[s],mintime[s],stp[s], etp[s]/stp[s]-1} }'  zzz_tmp
Result with your sample data:
[tt]company1 20060523 55 19900112 39 0,410256
company2 20050616 32 19980223 44 -0,272727[/tt]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top