Yes, you could use some subroutines. I always look for a way to wrap things into objects when indexing gets this messy. However, you don't have to use objects in this case. Here is a way to simplify life with some variables protected in a scope.
use strict; # I presume these will be set to something. my (@blocks, @datapoint, $radius); { # Scope to protect local variables. my ($blockcount, $icount); sub set_blockcount { $blockcount = shift; } sub set_icount { $icount = shift; } sub get_block { my ($final_index) = @_; return $blocks[$blockcount][$icount][$final_index]; } sub set_block { my ($final_index, $value) = @_; $blocks[$blockcount][$icount][$final_index] = $value; } sub get_next_block { my ($final_index) = @_; return $blocks[$blockcount][$icount + 1][$final_index]; } sub get_data { my ($b_index, $d_index) = @_; return $datapoint[($blocks[$blockcount][$icount][$b_index])][$d_ +index]; } } # end of scope to protect local variables # These are not the same as the variables within the subroutine scope. my $blockcount = 0; my $icount = 0; set_blockcount($blockcount); set_icount($icount); # Is this meant to be a loop? It isn't one without a while or for. { my $xachange = get_block(1) - get_data(5,1); my $xbchange = get_data(5,1) - get_next_block(1); my $yachange = get_block(2) - get_data(5,2); my $ybchange = get_data(5,2) - get_next_block(2); my $da = sqrt(($xachange*$xachange) + ($yachange*$yachange)); my $db = sqrt(($xbchange*$xbchange) + ($ybchange*$ybchange)); my $nxa = ((get_block(1) + ($xachange/$da)*$radius)); my $nya = ((get_block(2) + ($yachange/$da)*$radius)); my $nxb = ((get_block(1) + ($xbchange/$db)*$radius)); my $nyb = ((get_block(2) + ($ybchange/$db)*$radius)); my $gxa1 = $nxa / 25.4; my $gya1 = $nya / 25.4; my $gxb1 = $nxb / 25.4; my $gyb1 = $nyb / 25.4; my $gpx = get_data(5,1) / 25.4; my $gpy = get_data(5,2) / 25.4; select JOB; printf "G01X%.3fY%.3f\n", $gxa1, $gya1; printf "G03X%.3fY%.3fI%.3fJ%.3f\n", $gxb1, $gyb1, $gpx, $gpy; select STDOUT; print " 1 [4] G033333333333333333 $blockcount, $icount, 1 \n"; set_block(3,1); $icount = $icount + 1; set_icount($icount); }

In reply to Re: subroutine help by tall_man
in thread subroutine help by stu96art

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.