RE: Perl: removing an array element?

by "Brian A. Sayrs" <sayrs(at)southwindsolutions.com>

 Date:  Mon, 31 Jan 2000 18:23:00 -0800
 To:  "'Peter Newton'" <c-newton(at)ihug.co.nz>,
"hwg-techniques(at)hwg.org" <hwg-techniques(at)hwg.org>
  todo: View Thread, Original
Yeah...it's splice.  Here's the format:

splice ARRAY, OFFSET, LENGTH, LIST;

where ARRAY is the array you're chopping up, OFFSET is the number of the =
first element you want to get rid of, LENGTH is the number of elements =
to discard, and LIST are elements you want to put in their place.  =
LENGTH and LIST are optional, they default to 1 and (nothing).  So, =
here's your new code:

@array=3D split(/ /,"entry1 entry2 entry3 entry4 entry5 entry6");
print "array =3D @array\n";
$elementno=3D3;
splice (@array,$elementno);
print "array =3D @array\n";

the return value of splice is an array of the elements you removed, so =
you can keep the values, if you wish.

HTH,
Brian

-----Original Message-----
From:	Peter Newton [SMTP:c-newton(at)ihug.co.nz]
Sent:	Monday, January 31, 2000 5:12 PM
To:	hwg-techniques(at)hwg.org
Subject:	Perl: removing an array element?

Hello All,

Is their a SIMPLER way than this to remove a parlicular element from an
array:-

@array=3D split(/ /,"entry1 entry2 entry3 entry4 entry5 entry6");

print "array =3D @array\n";

$count=3D0;
$elementno=3D3;

foreach $line(@array){
 if ($count !=3D $elementno) {push (@newarray,$line);}
 ++$count;
}

$array=3D$newarray;
print "array =3D @array\n";

This seems very cumbersome it would be nice if their was something =
like:-

delete(@array[3]);   or
remove(@array,3);
or something like that

Many thanks
Peter Newton

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA

This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.