Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Shift on Multidimensional Array within Foreach Loop

Status
Not open for further replies.

seisner

Programmer
Feb 21, 2008
1
CA
Does anyone have any advice on how to chop off the first member in a multidimensional array?

Here's some sample data:

492000 200003 108352 2008
492000 200003 108352 2007
492000 200003 108352 2006
492000 200003 108352 2005

Here's some sample code:

foreach ( @memArray )
{

#do some stuff
shift(@memArray)
}

When I use shift within the foreach loop, it seems to be removing the wrong "rows" in the array. Has anyone had any experience with this? Any help would be much appreciated.
 
Something like this perhaps? You don't specify your array structure, so I've assumed an array of arrays...
Code:
use strict;
use warnings;
use Data::Dumper;

my @memArray;

push @memArray, [split] while (<DATA>);

print Dumper(@memArray);

shift @$_ foreach (@memArray); 

print Dumper(@memArray);

__DATA__
492000 200003 108352 2008
492000 200003 108352 2007
492000 200003 108352 2006
492000 200003 108352 2005

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
When I use shift within the foreach loop, it seems to be removing the wrong "rows" in the array.

In the code you posted you are shifting the top level array which will remove all the elements of each sub array. See Steves code above for the proper syntax for shifting the first element off of each sub array.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top