2009-11-05 88 views
0

試圖瞭解調用散列值的不同值的語法。 比如讓說,我想刪除「褲」如何去設置參數這樣的事情:散列方法參數值

products = {124 => ['shoes', 59.99], 352 => ['shirt', 19.99], 777 => ['pants', 19.87], 
667 => ['jacket', 39.99], 898 => ['shoulder_holster', 22.78]} 

在寫一個菜單驅動程序進行這個哈希我包括delete一個錯誤或之前檢查添加一個關鍵,這是我到目前爲止有:

if a == 3     # Loop delete a Product 
puts "Delete a Product" 
d = gets.to_s # Get value for argument 

    while products.has_value?( d + syntax for right here???? )!= true do 
    puts "This turned out false because product does not exsist!" 
    d = gets.to_s 
    end 

puts "Congrats your out of the loop" 
products.delete(d + again syntax problems ????) 
puts products 

如何進入語法的說法,如果我在哪裏可以刪除褲子。會是([d,:數字])我沒有運氣與在線任何資源與如何刪除或添加在這種情況下。任何幫助或代碼示例將不勝感激,

馬特

回答

1

這取決於用戶是否輸入了身份證號碼或姓名「褲子」。如果是前者:

if a == 3     # Loop delete a Product 
    puts "Delete a Product" 
    d = gets # Get value for argument 

    until products.has_key?(d.to_i) 
    puts "This turned out false because product does not exsist!" 
    d = gets 
    end 

    puts "Congrats your out of the loop" 
    products.delete(d.to_i) 
    puts products 
end 

如果它的「褲子」,那麼這就是你要如何做到這一點:

if a == 3     # Loop delete a Product 
    puts "Delete a Product" 
    d = gets.strip # Need to strip because otherwise the newline will wreck it 

    until products.find {|key, val| val.first == d} 
    puts "This turned out false because product does not exsist!" 
    d = gets.strip 
    end 

    puts "Congrats your out of the loop" 
    products.delete_if {|key, val| val.first == d} 
    puts products 
end 
1

寫了「從哈希刪除名爲產品」的方法

這樣做有它的短方法,但拍攝的清晰,我想出了這樣的:

products = {124 => ['shoes', 59.99], 352 => ['shirt', 19.99], 777 => ['pants', 19.87], 
667 => ['jacket', 39.99], 898 => ['shoulder_holster', 22.78]} 

def wipeProduct(hash, nameToDelete) 
    hash.each do |i| 
    key = i[0] 
    productName = i[1].first 
    hash.delete(key) if productName==nameToDelete 
    end 
end 

puts products.inspect 
wipeProduct(products,'pants') 
puts products.inspect 
wipeProduct(products,'shoulder_holster') 
puts products.inspect 

bash-3.2$ ruby prod.rb 
{352=>["shirt", 19.99], 898=>["shoulder_holster", 22.78], 667=>["jacket", 39.99], 777=>["pants", 19.87], 124=>["shoes", 59.99]} 
{352=>["shirt", 19.99], 898=>["shoulder_holster", 22.78], 667=>["jacket", 39.99], 124=>["shoes", 59.99]} 
{352=>["shirt", 19.99], 667=>["jacket", 39.99], 124=>["shoes", 59.99]} 

我不知道這是否是可能的「褲子」,在多個地方哈希發生,但因爲我用「hash.each(......)」,該方法wipeProduct(哈希,nameToDelete)將測試永遠y哈希條目。

輸入類型的錯誤,以及如何解決它

當你把輸入你指定你捕獲d的字符串。這裏的證明:

irb(main):010:0> d = gets.to_s 
12 
=> "12\n" 
irb(main):011:0> d.class 
=> String 

您可以在字符串轉換爲一個Fixnum這樣的:

irb(main):012:0> d.to_i 
=> 12 
irb(main):013:0> d.to_i.class 
=> Fixnum 

在產品散列的所有按鍵均Fixnums。這裏的證明:

irb(main):014:0> products.keys.each {|i| puts i.class} 
Fixnum 
Fixnum 
Fixnum 
Fixnum 
Fixnum 
=> [352, 898, 667, 777, 124] 

所以,你需要捕捉值與該行的說法:

d = gets.to_i # Get value for argument 

答案的缺失部分:

從產品,您可以用此編程刪除褲子條目:

products.delete(777)

運行它得到你:

您提供的關鍵值(在這種情況下777)到.delete()和它返回分別由以該順序的鍵和值的陣列
irb(main):003:0> products.delete(777) 
=> ["pants", 19.87] 

通知。

另一個實現

我不知道這是否是安全的。多數民衆贊成遍歷散列中的鍵值對塊修改散列。如果不是,你可以攢起來要刪除的所有鍵和遍歷哈希後刪除它們:

def wipeProduct(hash, nameToDelete) 
    keysToDelete = [] 
    hash.each do |i| 
    key = i[0] 
    productName = i[1].first 
    keysToDelete << key if productName==nameToDelete 
    end 
    keysToDelete.each {|key| hash.delete(key) } 
end 
2
products.to_a.select {|a| a.last.first == 'pants' } 

,將讓你匹配「褲」的紀錄。

[[777, ["pants", 19.87]]] 

所以我想你會想

while !products.to_a.select {|a| a.last.first == d }.empty? 
上你的循環

然後使用戴維德的行刪除的記錄。

1

下面是刪除 「褲子」 條目的更合適的方法:

def wipeProduct(hash, nameToDelete) 
    hash.reject!{|key,value| nameToDelete==value.first} 
end 

的拒絕!塊會查看每個鍵值對,並在其返回true時,所提供的鍵值將從哈希中移除。

0
if a == 3     # Loop delete a Product 
puts "Delete a Product by its key number" 
d = gets 
while products.has_key?(d)!= false do 
    puts "You have selected a key that is not currently in use" 
    d = gets 
end 
puts "You have deleted" 
products.delete(d) 
puts products 
end 

這是我最後做了一些麻煩與直到循環,所以交換了一段時間循環雖然因爲它不會接受新輸入的鍵由於某種原因

+0

btw感謝所有的信息傢伙肯定有幫助,而不是拉出頭髮 – Matt 2009-11-06 05:05:10