array help

J

JeremyWoertink

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns



~Jeremy
 
T

Thomas Adam

Hi --

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

[1,2,3][0..-2]
#=> [1, 2]

-- Thomas Adam
 
J

JeremyWoertink

well I guess 2 solutions would be either

[1,2,2] - [1,2,3].pop.to_a

which just seems really ugly and not rubyish at all.

then I guess I could do [1,2,3].first(2) as long as my array stays
constant, but if it changes, then i'm screwed.

Any other ideas?
 
J

JeremyWoertink

awesome. So what does the 0..-2 do? how does that work exactly?


Thanks for the help

~Jeremy

Hi --

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?
irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

[1,2,3][0..-2]
#=> [1, 2]

-- Thomas Adam
 
T

Thomas Adam

Hi --

awesome. So what does the 0..-2 do? how does that work exactly?

When you use array subscripts starting from -1, -2, -3, etc, it starts
to look at the _end_ of the array going backwards. So for instance:
=> [1, 2, 3]
=> 3

When you want a range inclusive of something, but not all the way to
end, you can tell it to stop off at a subscript which is relative to
the array's size. Since you didn't want the last element but
everything before it, that's -2 as an array subscript counting
backwards:
=> [1, 2]

-- Thomas Adam
 
B

Bertram Scharpf

Hi,

Am Freitag, 19. Okt 2007, 04:05:55 +0900 schrieb (e-mail address removed):
I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

In case you really need the array _and_ its reduced version
you could do somthing like

a = [1,2,3]
(d = a.dup).pop
d

The dup is inevitable and would be part of a inverse_pop
method if there were one.

Therefore I strongly recommend you consider another time
about what you want to achieve and how it is done best.
No harm meant,

Bertram
 
L

Lloyd Linklater

Jeremy said:
I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

I am not sure if you got your answer yet or not, so here goes.

What you are seeing is the result of the pop, i.e. what was popped.
What you want is the array AFTER the pop. This is how it goes:

a = [1, 2, 3]
p a.pop # 3 <= this is what was popped off
p a # [1, 2] <= This is what is left. use the array at this
point.
 
A

Anton Bangratz

--------------enig4B221558E003DF098B8FE353
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?
=20
irb(main):001:0> [1,2,3]
=3D> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=3D> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns
=20


Hello Jeremy,

your problem is a mite unclear, I'll try to cover a few possible
problems and their solutions

##
# Returning parts
irb(main):001:0> [1,2,3][0..1] # return the first two elements
=3D> [1, 2]
irb(main):004:0> [1,2,3].slice(0,2) # same as above, slice and [] are
in the end the same method
=3D> [1, 2]
irb(main):005:0> [1,2,3][0..-2] # return everything but the last
=3D> [1, 2]

# And now, the very 'interesting' way: map each value to it's own
# value, but to nil when it's '3' and remove the nil values from
# the array
irb(main):011:0> [1,2,3].collect() {|i| i unless i =3D=3D 3 }.compact
=3D> [1, 2]

##
# Manipulating in-place
irb(main):015:0> a =3D [1,2,3]
=3D> [1, 2, 3]
irb(main):016:0> a.delete(3) # delete everything with the value '3'
=3D> 3
irb(main):017:0> a
=3D> [1, 2]
irb(main):021:0> a =3D [1,2,3]
=3D> [1, 2, 3]
irb(main):022:0> a.delete_if {|i| i =3D=3D 3} # same as above
=3D> [1, 2]
irb(main):023:0> a
=3D> [1, 2]

# Delete the element at position '2', which incidentally is the last
# one
irb(main):028:0> a =3D [1,2,3]
=3D> [1, 2, 3]
irb(main):036:0> a.delete_at(2) # could also use (a.size-1)
=3D> 3
irb(main):037:0> a
=3D> [1, 2]

# Delete the element at the last position
irb(main):038:0> a =3D [1,2,3]
=3D> [1, 2, 3]
irb(main):039:0> a.delete_at(-1)
=3D> 3
irb(main):040:0> a
=3D> [1, 2]


=2E.. so far on a little fun with arrays. Note that in-place
manipulation actually works on the array itself, which might or might
not be what you want.

t.
--=20
Anton Bangratz - Key ID 363474D1 - http://tony.twincode.net/
fortune(6):
Q: What do they call the alphabet in Arkansas?
A: The impossible dream.


--------------enig4B221558E003DF098B8FE353
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQEVAwUBRxhILAgZMsE2NHTRAQK4QAf6AoybzouQusWC/kUm9B0kFtkrC7+0VZh8
b7YUr41dwaAaHIJeyjCl71owIcsuhVd1Gjs/d+tARPqh83TgQdadPcPkSaAmJhB5
QanOPeCvbdGc69m54f832KIVoVDa3alDTYfIUDVcFRzd/Viewb/KU4wbXMU83Mc1
bXFX5PebbZzURfjrhximLALdSWua1+AtKlwyf9cVM33eIizSsD6q24ob+d+GzcE/
KCa6HJFH+5m+SqiyaQjdwQjzT1h+sQ2Bl+OYLE4xuldFnuLFeUmbBALXIPFbjNPG
p4ACPgC0tvoRi91avP6+8vJqu6X0pDX8U9ZO8ZlRyPwzIKLIiTDjzw==
=/YDw
-----END PGP SIGNATURE-----

--------------enig4B221558E003DF098B8FE353--
 
B

Bertram Scharpf

Hi,

Am Freitag, 19. Okt 2007, 04:05:55 +0900 schrieb (e-mail address removed):
I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

Here's another one, just for entertainment:

class Array ; def evth_but_pop ; slice 0, length-1 ; end ; end
[1,2,3].evth_but_pop

Bertram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,025
Latest member
Rigor4

Latest Threads

Top