How to return an array from array of arrays using pop - Perl -
How to return an array from array of arrays using pop - Perl -
i have array of arrays in perl , need array @ rear end. used pop() , per description should homecoming lastly element in array(which array in case). notice pop() returns 2-d array(with 1 row , no. of columns equal no. of columns in lastly array had popped out). can somehow create pop() homecoming array instead of array of arrays?
do know perl references?
perl doesn't have info construction called array of arrays. have array, , each fellow member of array contains memory location of array.
when pop entry array, you're not getting array, references array.
i'm using data::dumper, see info structure:
use strict; utilize warnings; utilize feature qw(say); utilize data::dumper; @array = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], ); dumper \@array;
here's output:
$var1 = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
this array of arrays looks like..
let's utilize pop pull off lastly row:
#! /usr/bin/env perl # utilize strict; utilize warnings; utilize feature qw(say); utilize data::dumper; @array = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], ); $row_ref = pop @array; #row_ref _points to_ array. dumper $row_ref;
this prints out:
$var1 = [ 7, 8, 9 ];
so, pop
working. it's returns reference array. if need actual array, dereference reference:
use strict; utilize warnings; utilize feature qw(say); utilize data::dumper; @array = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], ); $row_ref = pop @array; # returns reference array @last_row = @{ $row_ref }; # dereferencing $row_ref @last_row # now, can treat @last_row our array $index ( 0..$#last_row ) { "\$last_row[$index] = $last_row[$index]"; }
this prints out:
$last_row[0] = 7 $last_row[1] = 8 $last_row[2] = 9
if you're not getting this, info construction isn't think is. utilize data::dumper
dump out.
arrays perl
Comments
Post a Comment