2

I am trying to solve a problem

Rahul is playing a very interesting game. He has N disks ( each of equal radius). Every disk has a distinct number out of 1 to N associated with it. Disks are placed one over other in a single pile.

Rahul wants to sort this pile of disk in increasing order from top to bottom. But he has a very very special method of doing this. In a single step he can only choose one disk out of the pile and he can only put it at the top.

Rahul wants to sort his pile of disks in minimum number of possible steps. So help Rahul in doing so. So don't have to show the actual steps just answer the minimum number of possible steps to sort the pile so that that Rahul can check whether he is doing his work right or wrong.

Code i am writing is

sub get_order { 
    my (@input1)= @_; 
    my @input2 = @input1;

    my $count = 0;
    sub recursive {
        my $max = 0;
        last if ( $#input2 == -1 ) ;
        foreach ( 0 .. $#input2) {
            $max = $max > $input2[$_] ? $max : $input2[$_];
            print " maximum is $max \n";
        }
        if ( $max == $input2[$max-1] ) { 
            $abc = 0;
        } else {
            $count++;
            #push @input2, $max;
        }
        # deleting that particular array index from the array
        my %hash = map { 
            $_ => "1" 
        } @input2;
        delete $hash{$max};
        print %hash;
        print "\n";
        @input2 = keys %hash;
        print "***@input2 \n";
        &recursive();
    }
    &recursive();
    print "value is $count \n"; 
    return $count;

 }

get_order(3,1,2);

I am getting a error Can't "last" outside a loop block at test.txt line 8.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Sumit
  • 1,953
  • 6
  • 32
  • 58
  • 2
    Looks like you're trying to solve the Tower of Hanoi problem: http://hop.perl.plover.com/book/pdf/01RecursionAndCallbacks.pdf – Zaid Sep 27 '13 at 14:03
  • @Zaid: I got these problems from a forum "techgig". Thought of solving this. :) – Sumit Sep 27 '13 at 14:04
  • 1
    You should avoid nesting subroutines. Better to use an anonymous subroutine instead. – Slaven Rezic Sep 27 '13 at 15:49
  • Great idea Slaven Rezic. But can you demonstrate little bit how to do that. Idea is looking interesting – Sumit Sep 27 '13 at 16:08
  • instead of `sub recursive {` => `my $recursive = sub {` and then `$recursive->()` to call it. Btw, drop `&` in front of function calls. – mpapec Sep 27 '13 at 16:28

2 Answers2

6

Outside of for, foreach, while, until, or a naked block, you can't use last, next or redo

Instead you can return from function,

return if ( $#input2 == -1 ) ;
brian d foy
  • 129,424
  • 31
  • 207
  • 592
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • 1
    It's not limited to `for` and `while` - you can `last` out of a bare loop: `perl -Mstrict -wE '{ last; say "test"; }'` – Zaid Sep 27 '13 at 14:02
  • @Zaid: What this means perl -Mstrict -wE '{ last; say "test"; }' – Sumit Sep 27 '13 at 14:02
  • Which part don't you understand? – ikegami Sep 27 '13 at 14:03
  • is this a one liner perl? I dont know one liner. What will -Mstrict do, what will -wE do and what is inside the braces – Sumit Sep 27 '13 at 14:05
  • @Nitesh : It's a test demonstrating that `last` works for a bare loop. Run it in your console - it should not print `"test"` because the `last` prevents execution of subsequent statements. – Zaid Sep 27 '13 at 14:05
  • 1
    `-M` is documented in perlrun. `-wE` is short for `-w -E`, documented in perlrun. Inside the brace is a `last` (which has already been covered, but also covered in perlfunc) and a `say` (which is covered in perlfunc). – ikegami Sep 27 '13 at 14:05
  • 2
    @Nitesh When in doubt, `perl -h` gives quick advice. – TLP Sep 27 '13 at 14:34
4

Part of the code is

sub recursive {
    my $max = 0;
    last if ( $#input2 == -1 ) ;
    foreach ( 0 .. $#input2) {

in these lines last does not occur within a for loop. Hence the error message.

last is used to exit from loop. If you want to exit from a sub then use return.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
AdrianHHH
  • 13,492
  • 16
  • 50
  • 87