Forum: Ruby Using and Querying Struct

Posted by Wayne Brissette (Guest)
on 2013-01-05 13:41
(Received via mailing list)
I'm trying to use the Stuct class to store some data. In my case, a 
bunch of data. But, in simplistic terms I've come up with the following 
example of what I'm looking for.

class Mailinglist <
  Struct.new(:f_name, :l_name, :city)
end

As an example, let's say I have two people John Smith and Jane Smith who 
live in Smithville. Is there an easy way to query an array containing 
Structs to determine of :l_name, and :city of with the array match and 
only produce one item from the array?

I know you could sort the array so all the data is sorted on l_name and 
then city. That would give you some order to the data:

#<Struct Mailinglist f_name="Mike", l_name="Jones", city="Pineville">
#<Struct Mailinglist f_name="Jane", l_name="Smith", city="Smithville">
#<Struct Mailinglist f_name="John", l_name="Smith", city="Smithville">
#<Struct Mailinglist f_name="Sally", l_name="Taulton", city="Ashville">

but would you have to iterate through the array to find all the matches 
to city, then l_name? I wouldn't think so, but I'm not sure how else to 
do it at this point.

Wayne
Posted by unknown (Guest)
on 2013-01-05 15:16
(Received via mailing list)
Am 05.01.2013 13:40, schrieb Wayne Brissette:
> I'm trying to use the Stuct class to store some data. In my case, a
> bunch of data. But, in simplistic terms I've come up with the
> following example of what I'm looking for.
>
> class Mailinglist < Struct.new(:f_name, :l_name, :city) end
>

There is no need to subclass, just use

   Mailinglist = Struct.new(:first_name, :last_name, :city)

(Btw, your class name is misleading, `ListMember' would be
more accurate.)

> As an example, let's say I have two people John Smith and Jane Smith
> who live in Smithville. Is there an easy way to query an array
> containing Structs to determine of :l_name, and :city of with the
> array match and only produce one item from the array?

   data = [
     Mailinglist.new('Mike', 'Jones', 'Pineville'),
     Mailinglist.new('Jane', 'Smith', 'Smithville'),
     Mailinglist.new('John', 'Smith', 'Smithville'),
     Mailinglist.new('Sally', 'Taulton', 'Ashville')
   ]

   matches = data.select {|member| member.city == 'Smithville' &&
member.last_name == 'Smith' }
   matches.first
   # => #<struct Mailinglist first_name="Jane", last_name="Smith",
city="Smithville">

Regards,
Marcus
Posted by unknown (Guest)
on 2013-01-05 15:29
(Received via mailing list)
Am 05.01.2013 15:16, schrieb sto.mar@web.de:
>    matches = data.select {|member| member.city == 'Smithville' &&
> member.last_name == 'Smith' }
>    matches.first
>    # => #<struct Mailinglist first_name="Jane", last_name="Smith",
> city="Smithville">

If you really need only the first match, you can use #find instead of
#select:

   first_match = data.find {|member| member.city == 'Smithville' &&
member.last_name == 'Smith' }
Posted by Wayne Brissette (Guest)
on 2013-01-05 16:27
(Received via mailing list)
Wow, thanks Marcus!

I think will work perfectly for my needs. I've got a situation where I 
have register addresses that are identical, but are used differently, 
but the list I retrieve requires me to do some additional checks to 
determine what's what. I struggled with how to deal with this, so I 
looked at the Struct class to store things in an array, so I could pull 
out the needed info, but then couldn't quite wrap my head around how I 
could query the entire array without iterating through it.

Thanks for the help!

Wayne
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.