Awk code with associative arrays — array doesn't seem populated, but no error

只愿长相守 提交于 2020-01-06 08:48:29

问题


  1. Question: Why does it seem that date_list[d] and isin_list[i] are not getting populated, in the code segment below?

  2. AWK Code (on GNU-AWK on a Win-7 machine)

    BEGIN { FS = "," } # This SEBI  data set has comma-separated fields (NSE snapshots are pipe-separated)
    
    # UPDATE the lists for DATE ($10), firm_ISIN ($9), EXCHANGE ($12), and FII_ID ($5).
    ( $17~/_EQ\>/ )    {
        if (date[$10]++ == 0) date_list[d++] = $10;   # Dates appear in order in raw data
        if (isin[$9]++ == 0) isin_list[i++] = $9;     # ISINs appear out of order in raw data
        print $10, date[$10], $9, isin[$9], date_list[d], d, isin_list[i], i 
    }
    
  3. input data

    49290,C198962542782200306,6/30/2003,433581,F5811773991200306,S5405611832200306,B5086397478200306,NESTLE INDIA LTD.,INE239A01016,6/27/2003,1,E9035083824200306,REG_DL_STLD_02,591.13,5655,3342840.15,REG_DL_INSTR_EQ,REG_DL_DLAY_P,DL_RPT_TYPE_N,DL_AMDMNT_DEL_00
    49291,C198962542782200306,6/30/2003,433563,F6292896459200306,S6344227311200306,B6110521493200306,GRASIM INDUSTRIES LTD.,INE047A01013,6/27/2003,1,E9035083824200306,REG_DL_STLD_02,495.33,3700,1832721,REG_DL_INSTR_EQ,REG_DL_DLAY_P,DL_RPT_TYPE_N,DL_AMDMNT_DEL_00
    49292,C198962542782200306,6/30/2003,433681,F6513202607200306,S1724027402200306,B6372023178200306,HDFC BANK LTD,INE040A01018,6/26/2003,1,E745964372424200306,REG_DL_STLD_02,242,2600,629200,REG_DL_INSTR_EQ,REG_DL_DLAY_D,DL_RPT_TYPE_N,DL_AMDMNT_DEL_00
    49293,C7885768925200306,6/30/2003,48128,F4406661052200306,S7376401565200306,B4576522576200306,Maruti Udyog Limited,INE585B01010,6/28/2003,3,E912851176274200306,REG_DL_STLD_04,125,44600,5575000,REG_DL_INSTR_EQ,REG_DL_DLAY_P,DL_RPT_TYPE_N,DL_AMDMNT_DEL_00
    49294,C7885768925200306,6/30/2003,48129,F4500260787200306,S1312094035200306,B4576522576200306,Maruti Udyog Limited,INE585B01010,6/28/2003,4,E912851176274200306,REG_DL_STLD_04,125,445600,55700000,REG_DL_INSTR_EQ,REG_DL_DLAY_P,DL_RPT_TYPE_N,DL_AMDMNT_DEL_00
    49295,C7885768925200306,6/30/2003,48130,F6425024637200306,S2872499118200306,B4576522576200306,Maruti Udyog Limited,INE585B01010,6/28/2003,3,E912851176274200306,REG_DL_STLD_04,125,48000,6000000,REG_DL_INSTR_EU,REG_DL_DLAY_P,DL_RPT_TYPE_N,DL_AMDMNT_DEL_00
    
  4. output that I am getting

    6/27/2003 1 INE239A01016 1  1  1
    6/27/2003 2 INE047A01013 1  1  2
    6/26/2003 1 INE040A01018 1  2  3
    6/28/2003 1 INE585B01010 1  3  4
    6/28/2003 2 INE585B01010 2  3  4
    
  5. Expected output

    As far as I can tell, the print is printing out correctly (i) $10 (the date) (ii) date[$10), the count for each date (iii) $9 (firm-ID called ISIN) (iv) isin[$9], the count for each ISIN (v) d (index of date_list, the number of unique dates) and (vi) i (index of isin_list, the number of unique ISINs). I should also get two more columns -- columns 5 and 7 below -- for date_list[d] and isin_list[i], which will have values that look like $10 and $9.

    6/27/2003 1 INE239A01016 1  6/27/2003 1 INE239A01016  1
    6/27/2003 2 INE047A01013 1  6/27/2003 1 INE047A01013  2
    6/26/2003 1 INE040A01018 1  6/26/2003 2 INE040A01018  3
    6/28/2003 1 INE585B01010 1  6/28/2003 3 INE585B01010  4
    6/28/2003 2 INE585B01010 2  6/28/2003 3 INE585B01010  4
    

回答1:


actual code I now use is

{    if (date[$10]++ == 0) date_list[d++] = $10;                 
     if (isin[$9]++ == 0) isin_list[i++] = $9;}        
( $11~/1|2|3|5|9|1[24]/ )) { ++BNR[$10,$9,$12,$5]}         
END { { for (u = 0; u < d; u++)  
      {for (v = 0; v < i; v++) 
      {    if  (BNR[date_list[u],isin_list[v]]>0) 
               BR=BNR[date_list[u],isin_list[v]] 
              { print(date_list[u], isin_list[v], BR}}}}}  

Thanks a lot to everyone.



来源:https://stackoverflow.com/questions/26086976/awk-code-with-associative-arrays-array-doesnt-seem-populated-but-no-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!