in reply to Re^3: How to speed up a nested loop?
in thread How to speed up a nested loop?

What are you talking about? If Grandfather's code works, then my code should work as well because it is just a trivial refactoring. If it does not work, then I guarantee that it is the result of a silly typo and not a result of my not understanding how to use references in Perl. If I've done something you don't understand, the odds are higher that I understand something about references in Perl that you don't, than the reverse.

If you doubt my competence, I suggest that you browse 10 or 20 of my best nodes. (You might want to skip the first couple, at least half of the top 10 don't have real programming meat.)

On using type as a constant, the usual convention is that constants should be ALL_CAPS. It doesn't matter to Perl, but you are violating expectations there.

Replies are listed 'Best First'.
Re^5: How to speed up a nested loop?
by Xenofur (Monk) on Sep 18, 2008 at 16:19 UTC
    What i don't understand in your code is what the purpose of this is meant to be:
    my $tiles_for_rx = $tiles[$rx];
    My understanding is that this dumps the size of $tiles[$rx] in $tiles_for_rx, due to being a multi-dimensional array. I may be wrong there, but the debugger seems to agree.

    However my understanding or lack of is not the point here. The point is that your variant did not work in the live when applied to my code. This works: When i try to refactor it to look like your example, it doesn't. Now mind you, i may be making some kind of mistake somewhere, but i'm fairly sure that your variant can't work. Feel free to correct me if i am making a mistake.

      I may be wrong there, but the debugger seems to agree.

      I don't see how the debugger can agree with that. Let's set up a multidimensional array and see what happens!

      use Data::Dumper; my @multi; foreach my $i ( 10 .. 12 ) { foreach my $j ( 10 .. 12 ) { $multi[$i-10][$j-10] = "$i,$j"; } } my $what_is_this = $multi[1]; print Dumper $what_is_this; __END__ $VAR1 = [ '11,10', '11,11', '11,12' ];

      The code in question actually gets a reference to an array, which is what's output by Data::Dumper.

      To get the number you're talking about, the syntax would be:

      my $n = scalar @{ $multi[1] };

      The use of scalar is not needed functionally, but it makes it easier to see what's meant.

        Ok, i got the problem now. The difference is that initially the tiles array is undefined, which makes an explicit grab of a reference inside it work different than an implicit. Me thinking $tiles_for_rx getting the size was caused by it being an empty scalar after the assignment, which made sense considering that tiles was at that point still empty.