in reply to Extraction of List of Coordinates

You're using a hash, but you're treating it as an array. Here's your code with the fix, and using an array.

foreach $element (@list_of_Num_Extraction) { if ($element =~ m/^([0-9]+),([0-9]+)$/) { my $first_num = $1; my $second_num = $2; my $current_num = ($first_num-1)*$NumOfColumns + $second_num; $Unique_Num_List[$current_num] = "($first_num,$second_num)"; } elsif ($element =~ m/^([0-9]+),([0-9]+):([0-9]+),([0-9]+)$/) { my ($num1x,$num1y,$num2x,$num2y) = ($1,$2,$3,$4); # Swap if ($num1x > $num2x) { $num1x ^= $num2x; $num2x ^= $num1x; $num1x ^= $num2x; } if ($num1y > $num2y) { $num1y ^= $num2y; $num2y ^= $num1y; $num1y ^= $num2y; } my $counter1 = 0; my $counter2 = 0; foreach $counter1 ($num1x .. $num2x) { foreach $counter2 ($num1y .. $num2y) { my $current_num = ($counter1-1)*$NumOfColumns + $counter2; $Unique_Num_List[$current_num] = "($counter1,$counter2)"; }} } } }

Replies are listed 'Best First'.
Re^2: Extraction of List of Coordinates
by Roy Johnson (Monsignor) on Oct 06, 2004 at 02:39 UTC
    In Perl, swaps can be accomplished thus:
    if ($num1x > $num2x) { ($num1x, $num2x) = ($num2x, $num1x) }

    Caution: Contents may have been coded under pressure.
      I was going to point out the same thing, and that doing it the Perl way is actually faster, but it turns out ikegami's XOR trick is slightly faster:
        After compensating for everything I could think of, XOR still won. Surprisingly, it won against the standard temp-variable swap. Incredibly, it won against a sub that simply returned the numbers in reverse order (in fact, temp swapping generally beat that, too!). I could only think that it wins because the integer math makes things simpler internally.

        I tried different types of arguments, and preprocessing the argument list to force it to integer. Speedup was slight, if there was any at all, for the integer-forced versions. Perl swapping lost pretty consistently in all cases. Temp swapping and return-value swapping beat XOR for swapping strings, sometimes. I got inconsistent results.

        Benchmark code follows...


        Caution: Contents may have been coded under pressure.