Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Changing an array from a sub

by jwkrahn (Monsignor)
on Feb 13, 2018 at 06:57 UTC ( #1209043=note: print w/replies, xml ) Need Help??

in reply to Changing an array from a sub

sub ChangeArray1 { my $i = 0; LOOP: return if ($i >= @A); $A[$i++] = 1; # Write to global array goto LOOP; }

That may be a "loop" in Assembler but it is technically not a "loop" in Perl. A proper perl loop would look something like this:

sub ChangeArray1 { my $i = 0; LOOP: { $A[ $i ] = 1; redo LOOP if ++$i < @A; } }

Replies are listed 'Best First'.
Re^2: Changing an array from a sub
by hippo (Canon) on Feb 13, 2018 at 09:14 UTC

    Personally I prefer this formulation (with the same caveat about modifying the global):

    sub ChangeArray1 { $_ = 1 for @A; }

    which to me is both clear and concise. All subjective, of course.

    And for the benefit of the OP: Go To Statement Considered Harmful.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1209043]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2019-02-15 22:49 GMT
Find Nodes?
    Voting Booth?
    I use postfix dereferencing ...

    Results (95 votes). Check out past polls.