*Verrrry* cool examples--especially the 'map' in the first one.

Thanks!

Paul


7:26am, Todd W. wrote:

>
> "Paul Archer" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > Is there any (quick and easy) way to get a reverse range, like (10..1),
> > rather than a standard (1..10)? The catch is to *not* use 'reverse'.
> > I'm teaching Sun's perl course this week (DTP-250), and we were talking
> > about working on arrays. The book had an exercise that had the student
> > reverse an array by using pop (or shift, I don't remember). That section
> is
> > before we talk about 'reverse', and I thought you'd be able to do it like:
> > @array[0 .. $#array] = @array[$#array .. 0]
> > ...but of course, having the range count down doesn't work.
> >
> > Paul
>
> Reverse an array without using reverse():
>
> with an array slice and map():
> [EMAIL PROTECTED] trwww]$ perl
> @array = ( 1 .. 5 );
> @array = @array[ map abs(), -$#array .. 0 ];
> print( join("\n", @array), "\n" );
> Ctrl-D
> 5
> 4
> 3
> 2
> 1
>
> using splice(), pop(), and for():
> [EMAIL PROTECTED] trwww]$ perl
> @array = ( 1 .. 5 );
> splice( @array, $_, 0, pop @array ) for ( 0 .. $#array );
> print( join("\n", @array), "\n" );
> Ctrl-D
> 5
> 4
> 3
> 2
> 1
>
> or the C way (but skipping the temporary variable):
> [EMAIL PROTECTED] perl]$ perl
> @array = ( 1 .. 5 );
> for ( $a = 0, $z = $#array; $a < $z; $a++, $z-- ) {
>   ( $array[$a], $array[$z] ) = ( $array[$z], $array[$a] );
> # @array[ $a, $z ] = @array[ $z, $a ]; # works too
> }
> print( join("\n", @array), "\n" );
> Ctrl-D
> 5
> 4
> 3
> 2
> 1
>
> perl is soooo cool.
>
> Todd W.
>
>
>

--------------------------------------------------------------
"I'll say this about Linux: it's the first time I've seen Unix
on the right platform."--Steve Ballmer, president of Microsoft
(NB: Microsoft used to own SCO, which did, and still does,
produce a Unix for the Intel platform.)
--------------------------------------------------------------

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to