?

Log in

No account? Create an account
Ramblings Journals I Read Calendar The Dirt MegaZone's Waste of Time Older Older Newer Newer
I like perl - MegaZone's Safety Valve
The Ramblings of a Damaged Mind
zonereyrie
zonereyrie
I like perl
Why do I like Perl?
    for (0 .. $#{@$table}) {
	return $table->[$_][0] if ($value eq $table->[$_][1]);
    }

I know that's not especially impressive, but it was while writing that that I just felt "Ah, I'm enjoying what I'm doing. I like working with this language." I know I'm no great perl hacker, and I'm WAY rusty after a couple of years without doing much perl work, but I'm starting to remember things and hopefully will pick up some new tricks on this project. I just want to get to a point where it all works so I can debug...

I am: amused amused
Current Media: Boa - Drinking

10 STDOUT || STDIN
Comments
kimera From: kimera Date: March 27th, 2003 06:32 am (UTC) (Direct Link)

Yes, Perl is good.
zonereyrie From: zonereyrie Date: March 27th, 2003 07:58 am (UTC) (Direct Link)
Of course, I should'nt've used the $index anyway - that was a vestiage of the port from CF, it was there and I morphed the CFscript 'for' loop into Perl. One of the first things I tend to do when porting is just redeclare variables. After I posted I realized I was wasting a variable. :-) I'm sure this still isn't optimal.
for (0 .. $#{@$table}) {
  return $table->[$_][0] if ($value eq $table->[$_][1]);
} 
kimera From: kimera Date: March 27th, 2003 08:04 am (UTC) (Direct Link)
What is the for doing for you in this anyway? I assume $#{@$table} ends up being some kind of number. Why not just use a while loop to cycle through it until the end of the array -- assuming your reading data from a table into an array?
zonereyrie From: zonereyrie Date: March 27th, 2003 08:53 am (UTC) (Direct Link)
Actually I have a 2 dimensional array - things like this:
my @ApprovePurchaseTable =
    (["apNoError", "1",
      "No error has occurred.  This should only occur under testing"],
     ["apUnknownAcceptParameters", "50",
      "Unknown  parameters. Indicates an error in data exchange between the Cash Register and the Shop."]);


Yes, that array is TINY - it is the smallest by far of the arrays the function with that routine is calling.

It is going through the array, looking for the matching error number, then pulling out the first text field. Why the number, which is always uses as the index, isn't *first* is legacy. I was tempted to redo the arrays as I ported them, but then they'd be harder to keep in sync with the other languages.
zonereyrie From: zonereyrie Date: March 27th, 2003 08:57 am (UTC) (Direct Link)
Oh, as for $#{@$table} - $table is a reference to the array, set in something like:
    elsif ($session->{FunctionName} eq "ApprovePurchase") {
        $table = \@ApprovePurchaseTable;
    }


Say you have @array, $#array is the number of elements in the array. If you have a reference like $table, then @$table puts it into an array context, so $#{@$table} is the number of elements in the array referenced by $table.
kimera From: kimera Date: March 27th, 2003 09:01 am (UTC) (Direct Link)

Interesting.

I need to look up the $#array feature/command. That would come in handy sometimes when I have to do $array_len = @array, it would save me a vairable and maybe a even a command/step from the way I do it now.
elisteran From: elisteran Date: March 27th, 2003 03:31 pm (UTC) (Direct Link)
$#array is the last index of the array. That is, assuming that you're using 0-based arrays,
scalar(@array) == $#array+1.
(Actually, perldata has the invariant
scalar(@whatever) == $#whatever - $[ + 1;
).
You're asking for an off-by-one error if you think of it as the length.
zonereyrie From: zonereyrie Date: March 27th, 2003 03:42 pm (UTC) (Direct Link)
Right! Sorry, my bad. I'm using it correctly but explained it poorly.
elisteran From: elisteran Date: March 27th, 2003 03:38 pm (UTC) (Direct Link)
Use the aliasing directly:
foreach (@$table) {
  return $_->[0] if $value eq $_->[1];
}

(foreach and for are identical; I just prefer using foreach when I'm doing aliasing).
zonereyrie From: zonereyrie Date: March 27th, 2003 03:44 pm (UTC) (Direct Link)
Oh, ok, that walks the first index, so $_->[0] is like $array[$i]->[0]... Interesting.
10 STDOUT || STDIN