perl - Ordered hash of hashes - setting and accessing key/value pairs -


i want implement ordered hash value of each key value pair nested hash map. unable so. not getting errors nothing being printed.

use hash::ordered; use constant { lead_id => 44671 , lag_id => 11536 , start_time => time };  $dict_lead=hash::ordered->new; $dict_lag=hash::ordered->new;  open(my $f1,"<","tcs_07may_nse_fo") or die "cant open input file"; open(my $f2,">","bid_ask_".&lead_id) or die "cant open output file"; open(my $f3,">","ema_data/bid_ask_".&lag_id) or die "cant open output file";  while(my $line =<$f1>){     @data=split(/,/,$line);     chomp(@data);     ($tstamp, $instr) = (int($data[0]), $data[1]);      if($instr==&lead_id){       $dict_lead->set($tstamp=>{"bid"=>$data[5],"ask"=>$data[6]});     }     if($instr==&lag_id){       $dict_lag->set($tstamp=>{"bid"=>$data[5],"ask"=>$data[6]});     } } close $f1; foreach $key ($dict_lead->keys){     $spread=$dict_lead{$key}{"ask"}-$dict_lead{$key}{"bid"};     %hash=$dict_lead->get($key);     print $key.",".$hash{"ask"}."\n";     print $f2 $key.",".$dict_lead{$key}{"bid"}.","           .$dict_lead{$key}{"ask"}.",".$spread."\n"; } foreach $key ($dict_lag->keys){     $spread=$dict_lag{$key}{"ask"}-$dict_lag{$key}{"bid"};     print $f3 $key.",".$dict_lag{$key}{"bid"}.","           .$dict_lag{$key}{"ask"}.",".$spread."\n"; } close $f2; close $f3; print "ring destroyed in " , time() - &start_time   , " seconds\n"; 

the output printed on terminal :

1430992791,   1430992792,   1430992793,   1430992794,   1430992795,   1430992796,   1430992797,   1430992798,   1430992799,   ring destroyed in 24 seconds 

i realize first column of output able insert key in ordered hash. don't understand how insert hash value keys. how access values while iterating through keys of hash?

the output in file corresponding file handle $f2 is:

1430970394,,,0   1430970395,,,0   1430970396,,,0   1430970397,,,0   1430970398,,,0   1430970399,,,0   1430970400,,,0   

first of all, don't see why want use module keeps hash in order. presume want output ordered timestamp fields, , data reading input file ordered that, simple sort keys of ordinary hash , print contents in order without relying on incoming data being presorted

you have read explanation of why code isn't behaving should. how write solution behaves (although haven't been able test beyond checking compiles)

instead of hash, have chosen use two-element array contain ask , bid prices each timestamp. should make code run fractionally faster making simpler , easier read

it's noteworthy have added use autodie, makes perl check status of io operations such open , chdir automatically , removes clutter caused coding checks manually. have defined constant path root directory of files, , used chdir set working directory there. removes need repeat part of path , reduces length of remaining file path strings

#!/usr/bin/perl  use strict; use warnings; use 5.010; use autodie;  use hash::ordered;  use constant dir     => '../tcs_nse_fo_merged'; use constant lead_id => 44671; use constant lag_id  => 11536;  chdir dir;  $dict_lead = hash::ordered->new; $dict_lag  = hash::ordered->new;  {     open $fh, '<', 'tcs_07may_nse_fo';      while ( <$fh> ) {          chomp;         @data = split /,/;          $tstamp = int $data[0];         $instr  = $data[1];          if ( $instr == lead_id ) {             $dict_lead->set( $tstamp => [ @data[5,6] ] );         }         elsif ( $instr == lag_id ) {             $dict_lag->set( $tstamp => [ @data[5,6] ] );         }     } }  {   $file = 'ema_data/bid_ask_' . lead_id;   open $out_fh, '>', $file;    $key ( $dict_lead->keys ) {       $val = $dict_lead->get($key);       ($ask, $bid) = @$val;       $spread = $ask - $bid;       print join(',', $key, $ask), "\n";       print $out_fh join(',', $key, $bid, $ask, $spread), "\n";   } }  {   $file = 'ema_data/bid_ask_' . lag_id;   open $out_fh, '>', $file;    $key ( $dict_lag->keys ) {       $val = $dict_lead->get($key);       ($ask, $bid) = @$val;       $spread = $ask - $bid;       print $out_fh join(',', $key, $bid, $ask, $spread), "\n";   } }  printf "ring destroyed in %d seconds\n", time - $^t; 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -