2017年2月25日土曜日

Get JGB historical data from MOF - Part 2.




  1. Updated  the previous version to use foreach, join,push and regular expression. this is more like real Perl. 
  2. using "foreeach"  instead of "for", no longer denepds on the fixed length list.
  3. with "push" and "join", codes are more readable.
  4. prepare the condition clause after heisei era.



system("wget http://www.mof.go.jp/jgbs/reference/interest_rate/data/jgbcm_all.csv");
$file="./jgbcm_all.csv";
$outfile="> ./jgb_seireki.csv";

open(IN,$file) or die "$!";
open(OUT,$outfile) or die "$!";
print("# start\n");
print OUT "Date,oneY,twoY,threeY,fourY,fiveY,sixY,sevenY,eightY,nineY\n";
$line_num = 0;
while(<IN>){
  $line_num++;           # increment counter before "next" otherwise......
  next if($line_num <3); # skip iteration if counter is less than 3 to skip 1st and 2nd lines.
  $gengou = substr($_,0,1);   # pick up the first character of the line and store into $gengou.
  @buff = split(/,/, $_); # split the line by comma.
  @data = split(/\./, $buff[0]); # split the first field by period.
  $year = substr($data[0],1,length($data[0])); # pick up the numerical part of wareki data.
  if($gengou eq 'S'){  # if the first character = S, the data belongs to showa.
    $year=$year+1925;  # add 1925 to adjust as showa period started in 1926.
  }
  if($gengou eq 'H'){  # for the case of heisei until 2019.
    $year=$year+1988;  # adjust for heisei period.
  }
  # if($gengou eq 'Z'){  # for the case of after heisei. change 'Z' to the appropriate
  #   $year=$year+2018;  # adjust 
  # }
  $outbuff = ""; # initialize buffer to construct output
  my @interest = (); # the list to store zero padded interest rate
  foreach $i(@buff){  # pick up element
    if($i !~ /^[A-Z]/ and $i =~ /[0-9]/){ # if the element is all numeric.
      push(@interest,sprintf("%.3f", $i));  # zero padd and push into the array
    }else{
      next; # otherwise junmp to the next element
    }
  }
  $outbuff = join(",",@interest,);  # join array concat w/ comma.
  print OUT  "$year-$data[1]-$data[2],$outbuff\n";
}
###

close(IN);
close(OUT);
system("rm jgbcm_all.csv")

0 件のコメント: