--qlTNgmc+xy1dBmNv
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Apr 15, 2008 at 12:47:21AM +0900, Vincent Angeloni wrote:
> Hi and greetings to all group members!
>=20
> I'm a new Ruby user with a background in Applescript, Hypertalk
> (Supercard), and a little Unix. I was intrigued by Matt Neuburg's
> article about Applescript and Ruby, and that's what got me reading some
> intro books on Ruby. I just bought Textmate and I am lovin' it!
>=20
> Anyway, I am wondering why this seemingly simple script fails:
>=20
> sizeList =3D [0,1,2,3,4,5,6]
> countarray =3D [3,4,6]
> countarray.each {|x| sizeList.delete_at(x)}
>=20
> what I want to happen is that sizelist gets deleted at the positions
> specified in countArray, but the result I am getting is:
>=20
> 0
> 1
> 2
> 4
> 6
>=20
> and the expected result should be 0,1,2,5, right?
> What gives?

When you delete_at(3), your new array looks like:

  sizeList[0] =3D 0
  sizeList[1] =3D 1
  sizeList[2] =3D 2
  sizeList[3] =3D 4
  sizeList[4] =3D 5
  sizeList[5] =3D 6

=2E . . because sizeList[3] =3D 3.

When you then delete_at(4), your new array looks like:

  sizeList[0] =3D 0
  sizeList[1] =3D 1
  sizeList[2] =3D 2
  sizeList[3] =3D 4
  sizeList[4] =3D 6

=2E . . because sizeList[4] =3D 5.  Et cetera.  Every time you delete an
element out of the middle, you shift everything to the right of it
leftward, basically.

You could try this:

  sizeList =3D [0,1,2,3,4,5,6]
  countarray =3D [6,4,3]
  countarray.each {|x| sizeList.delete_at(x)}

That would start at the far end of the array and work backwards, so that
positional shifts after a deleted element won't affect numbering for
stuff you want to delete later.  If you can't guarantee that the numbers
in countarray will be in reverse numerical order when you get them, you
can sort them that way thusly:

  countarray =3D [3,6,4].sort.reverse

If you want to keep the array intact, rather than removing elements, and
just reset the values of specific elements to be "nothing", you could
instead do something like this:

  countarray.each {|x| sizeList[x] =3D nil}

There are probably more elegant ways to do what you want -- and I'm not
entirely sure what you're trying to accomplish anyway -- but this might
help get you on the right track.

--=20
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Rudy Giuliani: "You have free speech so I can be heard."

--qlTNgmc+xy1dBmNv
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.8 (FreeBSD)

iEYEARECAAYFAkgD4DYACgkQ9mn/Pj01uKWgNgCdEl+2W8NfQgZd8d1zJmGjX8ta
s/wAoIpFr/NyzzY4d9KZoxZ0EY4nSBpi
=xvqu
-----END PGP SIGNATURE-----

--qlTNgmc+xy1dBmNv--