SPUG: re-size array

Brian Hatch bri at ifokr.org
Mon Oct 11 12:34:17 CDT 2004



> I need your help. Any body know what is the best way to re-size array. For example:
> 
> @array=(1,2,3,4,5,6,7,8,9,10);
> 
> I need to remove 1,3,5,7,9 and I need to re-size @array to:
> 
> @array=(2,4,6,8,10);
> 
> the array I need to re-size or re-defined has 166771 elements. I really appreciate all your help.

Are you splicing out based on the value, or just the position?

For example this would work if you just want to remove every other
element:

	@array=(1..10);
	@array = grep { %i++ % 2 }  @array;

Or if it's value based

	@array = (blahblahblah);
	@array = grep { doiwanttokeepit($_) } @array;

	sub doiwanttokeepit {
		my($value) = @_;
		if ( we want to keep this value ) {
			return 1;
		}
		return 0;
	}

-- 
Brian Hatch                  # apt-get remove George.W.Bush
   Systems and
   Security Engineer
http://www.ifokr.org/bri/

Every message PGP signed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.pm.org/archives/spug-list/attachments/20041011/d6c78575/attachment.bin


More information about the spug-list mailing list