in reply to I want to print a limited subsection of certain outputs?

Hi listendohg,

I see that you figured out a solution. Here's a way that you might find simpler using array slices:

#/usr/bin/perl use strict; use warnings; use feature 'say'; my @x = ( 1 .. 100 ); my @y = @x[ 0 .. 9 ]; my @z = @x[ -10 .. -1 ]; say join ',', @y; say join ',', reverse @z; __END__
Output:
1,2,3,4,5,6,7,8,9,10 100,99,98,97,96,95,94,93,92,91
If you don't know how many elements there are in your array:
my @y = @x[ 0 .. 9 ]; my @z = @x[ $#x-9 .. $#x ];

Note that unlike in your solution which makes new lists, changing the values in @y or @z will change the original array @x.

Hope this helps!

Edit: add example without specified indices.

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: I want to print a limited subsection of certain outputs?
by listendohg (Novice) on Feb 29, 2016 at 03:27 UTC
    Interesting, I'll look into this approach when I have to do this again. The code doesn't take long to write so I might as well start from scratch next time.