On 9/18/06, Palit, Nilanjan <[EMAIL PROTECTED]> wrote:
> That works too (& I have used it as well), but I guess what I'm really
> asking (hoping?) for is a Perl special variable, kinda like "$.", which
> Perl auto-magically initializes & increments ...

As was pointed out, any desired magic variable *can* be built at some
serious speed penalty compared to a built-in, but that doesn't seem
like the right solution either.

What is the *PURPOSE* of your push on newarray? Are you perhaps trying
to push each item of oldarray into a sub array (row) of newarray?  It
sounds like you're trying to transpose a matrix.

If so, and they're numeric, Perl modules for handling matrices may be
helpful [http://search.cpan.org/search?query=Matrix+transpose] -- or
PDL, the Perl Data Language [http://search.cpan.org/search?query=PDL],
if they're big numeric matrices.

If all the oldarrays are available in a outter array, as with a matrix
to be transformed, but the elements are non-numeric, or it's small and
you don't need all of a matrix lib,  you can map over the outter array
BUT doing it in new-array order to solve your problem efficiently, no
index arithmetic  -

#! perl -l

use strict;
use warnings;

my @newarray;
my @outteroldarray= @{[ [1, 2, 3,], [4, 5, 6,], [7, 8, 9,], ]}; ## or
[qw{a b c}], ...

use Data::Dumper;
print Dumper([EMAIL PROTECTED]);

push @newarray, [map { shift @$_ } @outteroldarray ]
    while grep {scalar @$_} @outteroldarray;

print Dumper [EMAIL PROTECTED];
### EOF ###

prints
$old = [
         [
           1, 2, 3
         ], [
           4, 5, 6
         ], [
           7, 8, 9
         ]
       ];

$new = [
         [
           1, 4, 7
         ], [
           2, 5, 8
         ], [
           3, 6, 9
         ]
       ];


With ragged rows, e.g, 6, 4, 5 elements in 3 rows such as
   my @outteroldarray= @{[ [qw{a b c d e f}], [qw{g h i j}], [qw{k l m
n o}], ]};
result in 6 rows of 3 elements, *internally* undef-padded as needed -
(
  [ 'a', 'g', 'k' ],
  [ 'b', 'h', 'l' ],
  [ 'c', 'i', 'm' ],
  [ 'd', 'j', 'n' ],
  [ 'e', undef, 'o' ],
  [ 'f', undef, undef ] );
which may not be what you want, or might be.

Side note --
   my @array=whatever();
  for my $i (0..$#array) {  doSomething($i, $array[$i], $array[f($i)] );   }
is the way to get the index w/o using C-style for, when you really
need index arithmetic. (If you don't need the $array[f($i)] term, you
probably don't need to loop over both $i and @array together if you
think map/grep enough.)

Note : This 0..$#array would have been INefficient for big array back
before for(0..$N) was optimized with a lazy list.
And note: "for" and "foreach" keywords are interchangeable, either can
take either set of parameters.

-- 
Bill
[EMAIL PROTECTED] [EMAIL PROTECTED]
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to