On Apr 7, 2005 8:54 AM, Vladimir D Belousov <[EMAIL PROTECTED]> wrote:
> Hallo, all!
>
> I have a function which prints array:
>
> sub print_array {
> my $array_ref = shift;
> for(@$array_ref){
> print "$_\n";
> }
> }
>
> And I have a big array, for example named @array.
> I want to print @array elements from N to N+100 using this function, but
> don't want to use second array (they are can be very big). After print I
> don't need @array anymore.
>
> I do this:
> $#array = $N+100;
> print_array($array[$N]);
>
You probably don't want this. This gives leaves you with an array
with $N+100 itema, but the items are $array[0] to $array[$N+100], not
$array[$N] to $array[$N+100].
To print, use a slice:
foreach (@$array_ref[$n..$n+100]) {
print "$_ somthing\n" ;
}
If you want to resize the array, don't forget you're using array
references here.
To splice (returns elements $N to $N+100): '@$arrary_ref =
splice(@$arrary_ref, $N, 100)'
To resize (leaves elements 0 to $N+100): '$#{$array_ref} = $N + 100'
Also, don't forget that modifying $# for an array will create undef
elements if they don't already exist, which can lead to strings of
"use of _ on uninitialized value" errors.
HTH,
--jay
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>