I'm sorry, as I can see I have explained incorrectly a problem.
In main() function  I have an array (not the array reference).
But function print_array() takes a reference to a part of array.

I want to give to print_array() function a reference to a part of array.
Array and that part of array may be very big and I wish to avoid excessive copying of data. Therefore I don't want use splice function.



Jay Savage wrote:

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






Reply via email to