convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

Below is part of the program that I am working on and I am stuck at line 98. Once you build a hash key based on another variable (on line 122), how do I send that info to sub run_ccr so that I can extract the value off based on that key? Currently, i am getting "Uninitialized value" error when I run the program. Also, any other comments are appreicated as I am building the program but not sure if I am doing things correctly or if I am doing things effiencely(i am sure i am not).
1 #!/usr/bin/perl -w 2 3 use strict; 4 use Text::CSV_XS; 5 my (@data,@data_epstn); 6 my ($count, $count_e_pstn) = 0; 7 my ($dothat,$dothis,$e_pstn); 8 9 ############################################################## +# 10 # Define attribute for STOP +# 11 ############################################################## +# 12 my @attrs_stop = qw( _status 13 _gwname 14 _call_duration 15 _eoc_reason 16 _call_direction 17 _called_num 18 _i_pstn_trunk 19 _e_pstn_circuit); 20 21 ############################################################## +# 22 # Define attribute for ATTEMPT +# 23 ############################################################## +# 24 my @attrs_attempt = qw( _status 25 _gwname 26 _eoc_reason 27 _call_direction 28 _called_num 29 _i_pstn_trunk 30 _i_pstn_circuit 31 _e_pstn_circuit 32 _e_pstn_trunk); 33 34 35 input (); 36 37 #sub stop { 38 # my @rec{@attrs_results} = @_[1,4,5]; 39 # return \%rec; 40 #} 41 42 #sub attempt { 43 # my @rec{@attrs_results} = @_[1,4,5]; 44 # return \%rec; 45 #} 46 47 ############################################################## +## 48 # Define sub for egress pstn + # 49 ############################################################## +## 50 sub epstn { 51 52 } 53 ############################################################## +## 54 # Define sub for igress pstn + # 55 ############################################################## +## 56 sub ipstn { 57 58 } 59 ############################################################## +## 60 # Define sub for extracting board info + # 61 ############################################################## +## 62 sub extract { 63 $_ = shift; 64 #print "\$\_ is $_\n"; 65 s/^1:([0-9]|[1-9][0-9]):([0-9]|[0-9][0-9]):(?:[0-9]:[0-9][0 +-9][0-9]*:\S+:\S+)/$1-$2/g; 66 return $_; 67 } 68 ############################################################## +## 69 # Define sub for input + # 70 ############################################################## +## 71 sub input { 72 my $csv = Text::CSV_XS->new; 73 while (<>) { 74 chomp; 75 next if /^START/; 76 $count++; 77 $csv->parse($_); 78 my @fields = $csv->fields; 79 my %rec; 80 my @attrs_results; 81 if (/^STOP/) { 82 @attrs_results = @attrs_stop; 83 # %rec = stop(@fields,@attrs_results); 84 @rec{@attrs_results} = @fields[0,1,13,14,16,20,33, +36]; 85 } else { 86 @attrs_results = @attrs_attempt; 87 # %rec = attempt(@fields,@attrs_results); 88 @rec{@attrs_results} = @fields[0,1,11,13,17,30,31, +33,57]; 89 } 90 push @data, \%rec; 91 } 92 } 93 ############################################################## +## 94 # sub ccr + # 95 ############################################################## +## 96 sub run_ccr { 97 foreach my $results1 (@data_epstn) { 98 print "$results1->{$e_pstn}\n"; 99 } 100 } 101 ############################################################## +## 102 # sub short call + # 103 ############################################################## +## 104 #sub sc ( 105 # ; 106 #} 107 ############################################################## +### 108 # Part of main program + # 109 ############################################################## +### 110 foreach my $info (@data) { 111 $dothis = $info->{_status}; 112 $dothat = $info->{_call_direction}; 113 if ($dothis eq 'STOP') { 114 #print "do nothing\n"; 115 next; 116 } elsif ("$dothat" eq 'IP-TO-PSTN') { 117 my %e_pstn; 118 $count_e_pstn++; 119 my $yahoo3 = $info->{_e_pstn_circuit}; 120 my @yahoo = extract($yahoo3); 121 my $yahoo1 = $info->{_e_pstn_trunk}; 122 $e_pstn = join ":", @yahoo,$yahoo1; 123 $e_pstn{$e_pstn} = $info->{_i_eoc_reason}; 124 push @data_epstn , \%e_pstn; 125 } else { 126 ; 127 } 128 } 129 130 print " There were $count calls\n"; 131 run_ccr();

Replies are listed 'Best First'.
Re: reference question between sub
by ikegami (Patriarch) on Sep 13, 2007 at 22:06 UTC

    Each element of @data_epstn of a reference to a hash containing a single element. That doesn't sound useful. I think your problem is much more fundamental. I'm not sure what you are trying to do, so I can't help design your structure.

    I do have a useful tidbit to give you: Data::Dumper can help you visual your data structures. For example, to view @data_epstn,

    use Data::Dumper; print Dumper \@data_epstn;

    To view $results1,

    use Data::Dumper; print Dumper $results1;
      Dumper helped. I made a typo, _i_eoc_reason , this was suppose to be _eoc_reason This program is not even close to being finished but so far looks ok... I always thought I should be using dumper but until you actually put it down there with how I should look at things, I dind't think it was usefull.. but it showed me what I was doing wrogn right away thank you!!
        actually, now I can see the structure, but my question still remains, how do I reiterate over hash reference which has dynamic key to get the value out?
        $VAR1 = [ { '13-3:USNJN4SOIMT_N3_N' => '16' }, { '15-73:USNJN4SOIMT_N3_N' => '34' }, { '13-84:USNJN4SOGLONAPUS_N' => '34' },