I have some code that creates a nested hash. In the code below, my goal is to write comma-separated output, with each line representing 2400 data points and 1 description for that chunk of data. I go through it and look at an array, and when I have enough information to print, I print it, otherwise, I keep going. The code works, but I'm trying to refactor the if-else block to make it more readable, robust and maintainable.

My thought now is to use a tabular ternary, where the return value is a string, either null or a row of data. I figure on this since a hash of subroutines won't really work - actions are not based on just one value.

Thoughts, anyone?

sub WriteSpinData { my ($OUT_FH) = shift @_; my ($ref_data) = shift @_; my ($last_major_frame, $line, @cur_spin ); #@cur_spin holds spin d +ata for latest chunk my ($row_length) = 15*160+1; for my $filename (sort keys %{ $ref_data }) { my ($basename) = basename $filename; for my $major_frame_index ( sort { $a <=> $b } keys %{ $ref_da +ta->{$filename} }) { ### Processing data for $basename [===| ] % d +one for my $minor_frame_index (keys %{ $ref_data->{$filename}- +>{$major_frame_index} }) { next if ($minor_frame_index !~ /\d+/); my @minor_frame_data = @{ $ref_data->{$filename}->{$m +ajor_frame_index}->{$minor_frame_index} }; my $cur_spin_cnt = (scalar @cur_spin > 0) ? scalar @cu +r_spin : 0; if ( ($cur_spin_cnt > 0) and ($cur_spin_cnt < $row_len +gth) ) { push @cur_spin, @minor_frame_data[3..17]; next; } elsif ($cur_spin_cnt == $row_length) { push @cur_spin, @minor_frame_data[3..17]; my $line = join ",", @cur_spin; print $OUT_FH "$line\n"; @cur_spin = (); next; } elsif ($cur_spin_cnt == 0) { if (($minor_frame_data[3] == 165) and (($minor_frame_index % 10) == 0) ) { my $index = "$filename,$major_frame_index,$min +or_frame_index"; @cur_spin = ($index, @minor_frame_data[3..17]) +; } next; } else { croak "should not be here - cur_spin_cnt $cur_spin +_cnt, $filename,$major_frame_index,$minor_frame_index\n"; } } $last_major_frame = $major_frame_index; } print "$basename: " . int($last_major_frame) . " major frames. +\n"; } }

-- Burvil


In reply to Refactoring nested if block by bowei_99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.