[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Alphabetizing

Zenki Nine

1/23/2008 5:22:00 AM

Hi,

I'm trying to write a code where I can alphabetize the players last_name
with its batting average

players_avg= << PLAYERS
Barry Bonds, .293
Mike Gallego, .263
Ichiro Suzuki, .396
Tony Gwynn, .403
Alex Rodriguez, .310
PLAYERS

players = presidents_avg.strip.collect { |players| players.strip
}.compact

#initialiating players_by_last_name with an empty array
players_by_last_name = []
--
Posted via http://www.ruby-....

11 Answers

7stud --

1/23/2008 7:30:00 AM

0

Zenki Nine wrote:
> Hi,
>
> I'm trying to write a code where I can alphabetize the players last_name
> with its batting average
>
> players_avg= << PLAYERS
> Barry Bonds, .293
> Mike Gallego, .263
> Ichiro Suzuki, .396
> Tony Gwynn, .403
> Alex Rodriguez, .310
> PLAYERS
>
> players = presidents_avg.strip.collect { |players| players.strip
> }.compact
>
> #initialiating players_by_last_name with an empty array
> players_by_last_name = []

Try this:

players_avg = <<PLAYERS
Barry Bonds, .293
Mike Gallego, .263
Ichiro Suzuki, .396
Tony Gwynn, .403
Alex Rodriguez, .310
PLAYERS

lines = players_avg.split("\n")
player_data = []

#chop lines into pieces
for line in lines
data = line.split(',')
names = data[0].split()
avg = data[1]
player_data << [names[0], names[1], avg]
end

#sort array by 2nd element, i.e. last name:
results = player_data.sort() do |arr1, arr2|
arr1[1]<=>arr2[1]
end

#output data:
results.each() do |arr|
puts "%s, %s %s" % [arr[1], arr[0], arr[2]]
end

--output:--
Bonds, Barry .293
Gallego, Mike .263
Gwynn, Tony .403
Rodriguez, Alex .310
Suzuki, Ichiro .396
--
Posted via http://www.ruby-....

Michael Fellinger

1/23/2008 8:05:00 AM

0

On Jan 23, 2008 2:22 PM, Zenki Nine <amateurdesigner@gmail.com> wrote:
> Hi,
>
> I'm trying to write a code where I can alphabetize the players last_name
> with its batting average
>
> players_avg= << PLAYERS
> Barry Bonds, .293
> Mike Gallego, .263
> Ichiro Suzuki, .396
> Tony Gwynn, .403
> Alex Rodriguez, .310
> PLAYERS

csv = CSV.parse(players_avg)
sorted = csv.sort_by{|p,a| p.split.last }
sorted.each{|p,a| puts("%20s: %5s" % [p, a]) }

Todd Benson

1/23/2008 7:26:00 PM

0

On Jan 22, 2008 11:22 PM, Zenki Nine <amateurdesigner@gmail.com> wrote:
> Hi,
>
> I'm trying to write a code where I can alphabetize the players last_name
> with its batting average
>
> players_avg= << PLAYERS
> Barry Bonds, .293
> Mike Gallego, .263
> Ichiro Suzuki, .396
> Tony Gwynn, .403
> Alex Rodriguez, .310
> PLAYERS
>
> players = presidents_avg.strip.collect { |players| players.strip
> }.compact
>
> #initialiating players_by_last_name with an empty array
> players_by_last_name = []

This isn't the best solution ... just there for s**ts and giggles...

irb(main):002:0> players = players_avg.split("\n").sort_by {|i|
(i.scan /\w*/)[2]}

Todd

Zenki Nine

1/23/2008 9:57:00 PM

0

Thanks All for helping me out on this..

Now..here's another issue..Say i want to display the retired players
last name in alphabetical order with its contents. Noticed this time
some has middle names, initials.

How would I tackle this?

Sorry, I've looked thru many books and have a difficult time solving
this..

retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

Thanks,
--
Posted via http://www.ruby-....

Phrogz

1/23/2008 10:06:00 PM

0

On Jan 23, 2:56 pm, Zenki Nine <amateurdesig...@gmail.com> wrote:
> Thanks All for helping me out on this..
>
> Now..here's another issue..Say i want to display the retired players
> last name in alphabetical order with its contents. Noticed this time
> some has middle names, initials.
>
> How would I tackle this?
>
> Sorry, I've looked thru many books and have a difficult time solving
> this..
>
> retired_players = <<PLAYERS
> Mike Anthony Gallego, 1985-1997
> Calvin Edwin Ripken, Jr., 1981-2001
> Tony K. Gwynn, 1982-2004
> Dennis Eckersley, 1975-1998
> PLAYERS

retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

# Break it into lines
players = retired_players.scan( /^.+/ )

# Turn each line into a real Player
Player = Struct.new( :name, :start_year, :end_year )
players.map!{ |line|
_, *pieces = line.match( /^(.+), (\d+)-(\d+)/ ).to_a
Player.new( *pieces )
}

# Sort as you see fit
players = players.sort_by{ |player|
# find the last name to use as the sort key
player.name[ /\w+(?:, (?:jr\.|sr\.|phd|dds))?$/i ]
}

puts players
#=> #<struct Player name="Dennis Eckersley", start_year="1975",
end_year="1998">
#=> #<struct Player name="Mike Anthony Gallego", start_year="1985",
end_year="1997">
#=> #<struct Player name="Tony K. Gwynn", start_year="1982",
end_year="2004">
#=> #<struct Player name="Calvin Edwin Ripken, Jr.",
start_year="1981", end_year="2001">

Phrogz

1/23/2008 10:27:00 PM

0

Slightly simpler than my previous:

retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

# Find player info: yields an array of arrays
players = retired_players.scan( /^(.+), (\d+)-(\d+)/ )

players = players.sort_by{ |name, start_year, end_year|
# find the last name to use as the sort key
name[ /\w+(?:, (?:jr\.|sr\.|phd|dds))?$/i ]
}

p players
#=> [["Dennis Eckersley", "1975", "1998"], ["Mike Anthony Gallego",
"1985", "1997"], ["Tony K. Gwynn", "1982", "2004"], ["Calvin Edwin
Ripken, Jr.", "1981", "2001"]]

Zenki Nine

1/23/2008 11:05:00 PM

0

Gavin Kistner wrote:
> Slightly simpler than my previous:
>
> retired_players = <<PLAYERS
> Mike Anthony Gallego, 1985-1997
> Calvin Edwin Ripken, Jr., 1981-2001
> Tony K. Gwynn, 1982-2004
> Dennis Eckersley, 1975-1998
> PLAYERS
>
> # Find player info: yields an array of arrays
> players = retired_players.scan( /^(.+), (\d+)-(\d+)/ )
>
> players = players.sort_by{ |name, start_year, end_year|
> # find the last name to use as the sort key
> name[ /\w+(?:, (?:jr\.|sr\.|phd|dds))?$/i ]
> }
>
> p players
> #=> [["Dennis Eckersley", "1975", "1998"], ["Mike Anthony Gallego",
> "1985", "1997"], ["Tony K. Gwynn", "1982", "2004"], ["Calvin Edwin
> Ripken, Jr.", "1981", "2001"]]

Thanks. Is there a way where I can display the full tenure? ie.
"1975-1998" instead?

I want to display in full:

'Dennis Eckersley, 1975-1998'

--
Posted via http://www.ruby-....

Lorenzo E. Danielsson

1/24/2008 1:05:00 AM

0

Zenki Nine wrote:
> Gavin Kistner wrote:
>> Slightly simpler than my previous:
>>
>> retired_players = <<PLAYERS
>> Mike Anthony Gallego, 1985-1997
>> Calvin Edwin Ripken, Jr., 1981-2001
>> Tony K. Gwynn, 1982-2004
>> Dennis Eckersley, 1975-1998
>> PLAYERS
>>
>> # Find player info: yields an array of arrays
>> players = retired_players.scan( /^(.+), (\d+)-(\d+)/ )
>>
>> players = players.sort_by{ |name, start_year, end_year|
>> # find the last name to use as the sort key
>> name[ /\w+(?:, (?:jr\.|sr\.|phd|dds))?$/i ]
>> }
>>
>> p players
>> #=> [["Dennis Eckersley", "1975", "1998"], ["Mike Anthony Gallego",
>> "1985", "1997"], ["Tony K. Gwynn", "1982", "2004"], ["Calvin Edwin
>> Ripken, Jr.", "1981", "2001"]]
>
> Thanks. Is there a way where I can display the full tenure? ie.
> "1975-1998" instead?
>
> I want to display in full:
>
> 'Dennis Eckersley, 1975-1998'
>

Replace:

p players

with:

players.each { |p, s, e|
puts "#{p}, #{s}-#{e}"
}


7stud --

1/24/2008 1:37:00 AM

0

Zenki Nine wrote:
> Thanks All for helping me out on this..
>
> Now..here's another issue..Say i want to display the retired players
> last name in alphabetical order with its contents. Noticed this time
> some has middle names, initials.
>
> How would I tackle this?
>
> Sorry, I've looked thru many books and have a difficult time solving
> this..
>
> retired_players = <<PLAYERS
> Mike Anthony Gallego, 1985-1997
> Calvin Edwin Ripken, Jr., 1981-2001
> Tony K. Gwynn, 1982-2004
> Dennis Eckersley, 1975-1998
> PLAYERS
>
> Thanks,


My attempt:


retired_players = <<PLAYERS
Mike Anthony Gallego, 1985-1997
Calvin Edwin Ripken, Jr., 1981-2001
Tony K. Gwynn, 1982-2004
Dennis Eckersley, 1975-1998
PLAYERS

#A container for each player's data:
Player = Struct.new(:last, :first_middle, :years)

#An array to hold each Player:
players = []

#Divide the string into lines:
lines = retired_players.split("\n")
lines.each do |line|
pieces = line.split(',')

#Extract the years played:
years = pieces[-1].strip()

#Extract the title, e.g. .jr, .sr
if pieces.length == 3 #then player has a title like .jr, .sr
title = pieces[1].strip()
else
title = false
end

#Extract the names:
names = pieces[0].split()
last = names[-1]
first_middle = names[0..-2].join(" ")
if title
last = sprintf("%s %s", last, title)
end

#Create a new Player with the above data:
players << Player.new(last, first_middle, years)
end

#Sort the Player objects in the players array by last name:
sorted_players = players.sort do |p1, p2|
p1.last<=>p2.last
end

#Output the sorted players:
sorted_players.each do |player|
puts sprintf("%s, %s %s", player.last, player.first_middle,
player.years)
end


--output:--
Eckersley, Dennis 1975-1998
Gallego, Mike Anthony 1985-1997
Gwynn, Tony K. 1982-2004
Ripken Jr., Calvin Edwin 1981-2001
--
Posted via http://www.ruby-....

Todd Benson

1/24/2008 6:49:00 AM

0

On Jan 23, 2008 3:56 PM, Zenki Nine <amateurdesigner@gmail.com> wrote:
> Thanks All for helping me out on this..
>
> Now..here's another issue..Say i want to display the retired players
> last name in alphabetical order with its contents. Noticed this time
> some has middle names, initials.
>
> How would I tackle this?
>
> Sorry, I've looked thru many books and have a difficult time solving
> this..
>
> retired_players = <<PLAYERS
> Mike Anthony Gallego, 1985-1997
> Calvin Edwin Ripken, Jr., 1981-2001
> Tony K. Gwynn, 1982-2004
> Dennis Eckersley, 1975-1998
> PLAYERS
>
> Thanks,

If you cannot identify the last name by position -- I don't know --
then maybe you need to use a database. I suppose you can grab the
first written word before a comma, but I'm not sure I'd trust that.

Todd