<?xml version="1.0" encoding="windows-1252"?>
<node id="161847" title="Re: seeking different ways to slice a 2-d array" created="2002-04-24 23:34:30" updated="2005-07-21 01:31:21">
<type id="11">
note</type>
<author id="2329">
stephen</author>
<data>
<field name="doctext">
&lt;p&gt;A &lt;em&gt;transitory variable&lt;/em&gt; is a variable that isn't used for anything other than to schlep data from one line to another. (I'm not sure the term is right, but I think it's what you mean. I call 'em "useless variables", myself.) In other words, if I'm going to say:
&lt;code&gt;
my $foo = get_foo();
do_something($foo);
&lt;/code&gt;
Then &lt;code&gt;$foo&lt;/code&gt; is a transitory variable. It would be just as simple, and less error-prone, to say:
&lt;code&gt;
do_something( get_foo() );
&lt;/code&gt;
&lt;p&gt;Why? Because &lt;em&gt;variables can and should vary.&lt;/em&gt; It doesn't seem like such a big deal when the assignment and the use of the variable are right next to each other, but what if they get separated?&lt;/p&gt;
&lt;code&gt;
my $foo = get_foo();
if ($snargle = 3 &amp;&amp; $foo = 2) {
   whatever($snargle, $foo);
   # lots of other stuff here...
}
do_something($foo);
&lt;/code&gt;
&lt;p&gt;Oops, on line 2 up there we changed the value of &lt;code&gt;$foo&lt;/code&gt;. Also, it makes refactoring harder-- what if you wanted to pull out the stuff inside the if statement into its own subroutine? It'd be simpler if you didn't have &lt;code&gt;$foo&lt;/code&gt; there at all, and just called &lt;code&gt;get_foo()&lt;/code&gt; when you needed a foo-value.
&lt;/p&gt;
&lt;p&gt;In your code, &lt;code&gt;@for_data&lt;/code&gt;, &lt;code&gt;@map_data&lt;/code&gt;, &lt;code&gt;@sort_data&lt;/code&gt;, and &lt;code&gt;@grep_data&lt;/code&gt; are all transitory variables. Ironically, &lt;code&gt;@last_elements&lt;/code&gt; is not... you're using it to collect elements! Nothing wrong with that.&lt;/p&gt;
&lt;p&gt;As for why &lt;code&gt;slce_with_sort()&lt;/code&gt; and &lt;code&gt;slice_with_grep()&lt;/code&gt; aren't doing what you expect, it's because sort and grep don't do what you apparently think they do. :) The block argument to grep is a test block, not a filter block. So the statement
&lt;code&gt;
grep { $_-&gt;[2] } @slice_me;
&lt;/code&gt;
means, "If the item at index 2 of &lt;code&gt;@$_&lt;/code&gt; evaluates to true, return &lt;code&gt;$_&lt;/code&gt;." The last bit is important-- it doesn't return the item at index 2, it returns the whole thing. A similar thing is happening with sort. You're sorting each row based on the value of the last column, and returning a list of the rows. Since the values are identical, it doesn't even change their order. :)&lt;/p&gt;
&lt;p&gt;So where did you get that water? Sounds like powerful stuff. :)&lt;/p&gt;

&lt;p&gt;
stephen
&lt;/p&gt;</field>
<field name="root_node">
161836</field>
<field name="parent_node">
161836</field>
</data>
</node>
