Re: Remove Parts of a String
- From: Victor Reyes <victor.reyes@xxxxxxxxx>
- Date: Sun, 1 Jun 2008 17:42:09 -0500
On 6/1/08, Eric I. <rubytraining@xxxxxxxxx> wrote:
already spoke, after all, I am just learning Ruby. However, I do find that
On Jun 1, 1:59 pm, Dan __ <major_general_...@xxxxxxxxxxx> wrote:
Eric I. wrote:
On Jun 1, 11:33�am, Dan __ <major_general_...@xxxxxxxxxxx> wrote:
Thanks very much Stefano :) �Thats exactly what I wanted, and it works
great :)
--
Posted viahttp://www.ruby-forum.com/.
Hi Dan,
You're the expert in your the domain, and I obviously don't understand
the larger picture. But the idea of getting access to the data after
the colons at a later stage using indexing into the original string
sounds unnecessarily complex. So here's another approach.
If you run this, you'll see what you get at every step.
====
s = "12343:3,73820:1,183874:8"
# break it up into an array of arrays of strings
a = s.split(',').map { |v| v.split(':') }
p a
# now you can do lots of
things
# just want the numbers before the
colons?
b1 = a.map { |v| v.first }
p b1
# want them as integers rather than
strings?
b2 = a.map { |v| v.first.to_i }
p b2
# want it as a string w/ commas? this seems to be part of your
original request
b3 = a.map { |v| v.first }.join(',')
p b3
# want to regenerate the original
string?
b4 = a.map { |v| v.join(':') }.join(',')
p b4
====
Maybe (or maybe not) that's helpful....
Eric
Hi Eric, thanks for the reply :)
Your solution seems great for more complex data uses. However, I'm
using this as part of a simple page-rating system in a Rails application
right now. Basically, the number before the colon is the ID of a page
that has been rated, and the number after the colon is the rating. The
entire string stores every page and rating the user has ever given. So
by splitting the string, and only looking at the number before the
colon, I can compare the ID of the current page to the IDs of the pages
the user has rated, and then display the rating they've given.
It seems to me that both yours and Stephano's solutions are equally
simple for what I'm using them for. If I had need to expand to a more
complex system, I'd definitely use yours. I'm fairly positive that my
use for the data won't expand beyond what it is now, and I've already
got Stephano's solution implemented, so I'm gonna stick with that one ;)
Thanks very much for offering your solution to the problem though :)
--
Posted viahttp://www.ruby-forum.com/.
Hi Dan,
You are most welcome! You know, it sounds like what you need is a
Hash, indexed by ID with the ratings being the values. Lookup becomes
trivial then. And if this is a case where the user logs in to your
Rails app, you can pull this string from your DB, convert it to a
hash, and store it in the session, so you don't have to re-process
that string each time.
Here's some more code (pretty much unsolicited at this point, eh?)
respectfully offered for you to consider:
====
ratings = "12343:3,73820:1,183874:8"
ratings_array = ratings.split(/[,:]/).map { |str| str.to_i }
# we now have array w/ ids & ratings as integers (not strings)
interleaved:
# [12343, 3, 73820, 1, 183874,
8]
ratings_by_id = Hash[*ratings_array]
# we convert that into a hash where keys are ids and values are
ratings:
# {12343=>3, 183874=>8,
73820=>1}
# and now we can look up an id and get either its rating or, if
there
# is no rating,
nil
r1 = ratings_by_id[183874]
p r1 # prints 8
r2 = ratings_by_id[77777]
p r2 # prints nil
====
I converted all the data to integers, but you could leave them as
strings if that made more sense for your app. But integers are better
from a performance standpoint for hash look-up.
Best,
Eric
====
LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE
workshops.
Ruby Fundamentals Wkshp June 16-18 Ann Arbor, Mich.
Ready for Rails Ruby Wkshp June 23-24 Ann Arbor, Mich.
Ruby on Rails Wkshp June 25-27 Ann Arbor, Mich.
Ruby Plus Rails Combo Wkshp June 23-27 Ann Arbor, Mich
Please visit http://LearnRuby.com for all the details.
Well, I am not sure if I should say anything at all after the BIG GUNs
to understand:
str.gsub(/:[^,]*(?:$|(?=,))/,'').split( ',')
you need a PhD in "General Expresionology". Ruby is supposed to be simple.
Here you have 1 line of code but need 1 page to explain it. I do find it
elegant, although I don't understand it. I only wish I could do that to!
Victor
- Follow-Ups:
- Re: Remove Parts of a String
- From: Dave Bass
- Re: Remove Parts of a String
- From: Stefano Crocco
- Re: Remove Parts of a String
- References:
- Remove Parts of a String
- From: Dan __
- Re: Remove Parts of a String
- From: Stefano Crocco
- Re: Remove Parts of a String
- From: Dan __
- Re: Remove Parts of a String
- From: Eric I.
- Re: Remove Parts of a String
- From: Dan __
- Re: Remove Parts of a String
- From: Eric I.
- Remove Parts of a String
- Prev by Date: Re: installing 1.8.7
- Next by Date: Re: Gateway -> Mail
- Previous by thread: Re: Remove Parts of a String
- Next by thread: Re: Remove Parts of a String
- Index(es):
Relevant Pages
|