How to crawl other hash in other hash loop - ruby

I have 2 hash in my Ruby code.
I wanna get data of "d" hash in "c" loop.
c = {"2"=>"20", "3"=>"30"}
d = {"2"=>"Du", "3"=>"Bist"}
c.each_with_index do |me,index|
puts me
end
output is:
2 20 3 30
I wanna get this output instead:
Du Bist

Do as below :
c = {"2"=>"20", "3"=>"30"}
d = {"2"=>"Du", "3"=>"Bist"}
c.each_with_index do |(k,v),i|
puts "#{d[k]} at index #{i}"
end
# >> Du at index 0
# >> Bist at index 1
# I don't know why you want each_with_index, instead of each here
# But here is how you can do.
c.each_with_index do |(k,v),i|
puts d[k]
end
# >> Du
# >> Bist
c and d are Hash.c.each_with_index do |me,index| here, with each itration me first has the value as ["2","20"], then ["3","30"]. Thus puts me printing it as 2 20 3 30. You need to have a look at Hash#[].

Related

How to delete double quotation mark from ruby array?

Based on the link
I tried to delete "" in the array on ruby
However still not get what I want, if anyone knows, please advice me
a = gets
lines = []
aaa = []
b = []
bb =[]
while line = gets do
lines << line.chomp.split(' ')
end
for k in 0..(lines.size - 1) do
b << lines[k][1].to_i + 1
end
for i in 0..(lines.size - 1)do
bb << lines[i][0] + ' ' + b[i].to_s
end
for l in 0..(lines.size - 1)do
p bb[l]
end
Input
3
Tanaka 18
Sato 50
Suzuki 120
Output
[["Tanaka", "18"], ["Sato", "50"], ["Suzuki", "120"]]
"Tanaka 19"
"Tanaka 19"
"Sato 51"
"Suzuki 121"
As pointed out in the comments, you can get rid of the quotation marks by replacing p (Ruby's inspect/print) with puts.
While we're at it, you can make this much more "Ruby-ish" by using .readlines to scoop up all the input into an array, and by replacing the multiple counting loops with .map or .each iterators. The following is more concise, and allows you to lose the first input line which you're just throwing away anyway.
lines = STDIN.readlines(chomp: true).map do |line|
l = line.split(' ')
[l[0], l[1].to_i + 1].join(' ')
# or
# "#{l[0]} #{l[1].to_i + 1}"
end
lines.each { |line| puts line }
With Ruby 3, you can use rightward-assignment for the first part if you find it more readable:
STDIN.readlines(chomp: true).map do |line|
l = line.split(' ')
"#{l[0]} #{l[1].to_i + 1}"
end => lines

trouble appending hash value ruby

I'm writing a program which takes input, stores it as a hash and sorts the values.
I'm having trouble comparing a current hash value with a variable.
Sample Input:
3
A 1
B 3
C 5
A 2
B 7
C 2
Sample Output:
A 1 2
B 3 7
C 2 5
Everything works apart from this part, and I'm unsure why.
if values.key?(:keys)
if values[keys] >= val
values.store(keys,val.prepend(val + " "))
else
values.store(keys,val.concat(" " + val))
end
else
values.store(keys,val)
end
i = i + 1
end
Rest of code:
#get amount of records
size = gets.chomp
puts size
size = size.to_i
values = Hash.new(0)
i = 0
while i < (size * 2)
text = gets.chomp
#split string and remove space
keys = text.split[0]
val = text.split[1]
#check if key already exists,
# if current value is greater than new value append new value to end
# else put at beginning of current value
if values.key?(:keys)
if values[keys] >= val
values.store(keys,val.prepend(val + " "))
else
values.store(keys,val.concat(" " + val))
end
else
values.store(keys,val)
end
i = i + 1
end
#sort hash by key
values = values.sort_by { |key, value| key}
#output hash values
values.each{|key, value|
puts "#{key}:#{value}"
}
Could anyone help me out? It would be most appreciated.
The short answer is that there are two mistakes in your code. Here is the fixed version:
if values.key?(keys)
if values[keys] >= val
values.store(keys,values[keys].prepend(val + " "))
else
values.store(keys,values[keys].concat(" " + val))
end
else
values.store(keys,val)
end
The if statement was always evaluating as false, because you were looking for hash key named :keys (which is a Symbol), not the variable you've declared named keys.
Even with that fixed, there was a second hidden bug: You were storing a incorrect new hash value. val.concat(" " + val) would give you results like A 2 2, not A 1 2, since it's using the new value twice, not the original value.
With that said, you code is still very confusing to read... Your variables are size, i, text, val, values, key and keys. It would have been a lot easier to understand with clearer variable names, if nothing else :)
Here is a slightly improved version, without changing the overall structure of your code:
puts "How may variables to loop through?"
result_length = gets.chomp.to_i
result = {}
puts "Enter #{result_length * 2} key-value pairs:"
(result_length * 2).times do
input = gets.chomp
input_key = input.split[0]
input_value = input.split[1]
#check if key already exists,
# if current value is greater than new value append new value to end
# else put at beginning of current value
if result.key?(input_key)
if result[input_key] >= input_value
result[input_key] = "#{input_value} #{result[input_key]}"
else
result[input_key] = "#{result[input_key]} #{input_value}"
end
else
result[input_key] = input_value
end
end
#sort hash by key
result.sort.to_h
#output hash result
result.each{|key, value|
puts "#{key}:#{value}"
}
h = Hash.new { |h,k| h[k] = [] }
input = ['A 1', 'B 3', 'C 5', 'A 2', 'B 7', 'C 2'].join("\n")
input.each_line { |x| h[$1] << $2 if x =~ /^(.*?)\s+(.*?)$/ }
h.keys.sort.each do |k|
puts ([k] + h[k].sort).join(' ')
end
# A 1 2
# B 3 7
# C 2 5
This would be a more Ruby-ish way to write your code :
input = "A 1
B 3
C 5
A 2
B 7
C 2"
input.scan(/[A-Z]+ \d+/)
.map{ |str| str.split(' ') }
.group_by{ |letter, _| letter }
.each do |letter, pairs|
print letter
print ' '
puts pairs.map{ |_, number| number }.sort.join(' ')
end
#=>
# A 1 2
# B 3 7
# C 2 5

How to count 1 to 9 on a single line in ruby

I'm struggling to figure out how to loop numbers in a single line on ruby.
x = 0
while x <= 9
puts x
x +=1
end
This would give you
0
1
2
3
4
5
6
7
8
9
Each on different lines.
But what I want is to get this on a single line so like
01234567891011121314151617181920
Also not limited to just 0-9 more like 0 to infinity on a single line.
The purpose is to make an triangle of any size that follows this pattern.
1
12
123
1234
12345
123456
Each of these would be on a different line. The formatting here won't let me put in on different lines.
Would really like to solve this. It is hurting my head.
try this:
(1..9).each { |n| print n }
puts
You said you want "to make a triangle of any size that follows this pattern", so you should not make assumptions about how that should be done. Here are two ways to do that.
#1
def print_triangle(n)
(1..n).each.with_object('') { |i,s| puts s << i.to_s }
end
print_triangle(9)
1
12
123
1234
12345
123456
1234567
12345678
123456789
#2
def print_triangle(n)
s = (1..n).to_a.join
(1..n).each { |i| puts s[0,i] }
end
print_triangle(9)
1
12
123
1234
12345
123456
1234567
12345678
123456789
how about this solution:
last_num = 9
str = (1..last_num).to_a.join # create string 123456789
0.upto(last_num-1){ |i| puts str[0..i] } # print line by line
puts (1..9).map(&:to_s).join
Regarding your final aim there are lots of (probably easier) ways, but here's one:
def print_nums k
k.times { |n| puts (1..(n+1)).map { |i| i*10**(n+1-i) }.inject(:+) }
end
print_nums 9
#1
#12
#123
#1234
#12345
#123456
#1234567
#12345678
#123456789
This approach generates the actual numbers using units, tens, hundreds etc in relation to the line number i.
Thought Process
Looking at a basic example of four lines:
1
12
123
1234
is the same as:
1*10**0 #=> 1
1*10**1 + 2*10**0 #=> 12
1*10**2 + 2*10**1 + 3*10**0 #=> 123
1*10**3 + 2*10**2 + 3*10**1 + 4*10**0 #=> 1234
which in Ruby can be generated with:
(1..1).map { |i| i*10**(1-i) }.inject(:+) #=> 1
(1..2).map { |i| i*10**(2-i) }.inject(:+) #=> 12
(1..3).map { |i| i*10**(3-i) }.inject(:+) #=> 123
(1..4).map { |i| i*10**(4-i) }.inject(:+) #=> 1234
looking for a pattern we can generalise and put in a method:
def print_nums k
k.times { |n| puts (1..(n+1)).map { |i| i*10**(n+1-i) }.inject(:+) }
end
You could (and should) of course ignore all of the above and just extend the excellent answer by #seph
3.times { |i| (1..(i+1)).each { |n| print n }; puts }
#1
#12
#123
The simplest way if you want to start from 1
9.times {|n| puts n+1}
try if you want to start from 0
10.times {|n| puts n}
if you want pyramid format this is one way to do
9.times{|c| puts (1..c+1).to_a.join}
this is the ouput
2.3.0 :025 > 9.times{|c| puts (1..c+1).to_a.join}
1
12
123
1234
12345
123456
1234567
12345678
123456789

Iterate through first n elements of array - if that many exist

I am looking for a more elegant, 'Ruby-ists' way to do the following. Is there any cleaner way to do this?
i=0
array.each do |x|
break if x.empty?
puts x
i+=1
break if i>4
end
I saw that you were calling #empty? on the elements in your array and quitting when you see the first empty element. If you want to preserve that behavior, you could do:
array.first(4).each do |x|
break if x.empty?
puts x
end
A fancier way would be:
array.take_while { |i| !i.empty? }.first(4).each do |i|
puts i
end
I am not sure how many elements you want to print; please note that my examples will print at most 4 elements, whereas your code was printing up to 5.
I'd suggest Array#first as follows:
array.first(4).each do |x|
puts x
end
If you only want to accept the first so many non-nil entries, then filter them out using Array#compact:
array.compact.first(4).each do |x|
puts x
end
If you are concerned about empty values then you could still chain the filters using Array#reject:
array.reject(&:empty?).first(4).each do |x|
puts x
end
Another way:
def printem(a,n)
puts a[0, [a.index(&:empty?) || n, n].min]
end
printem [[], [2],[3],[4],[5],[6]], 4
# <prints nothing>
printem [[1],[2], [],[4],[5],[6]], 4
# 1
# 2
printem [[1],[2],[3],[4],[5],[6]], 4
# 1
# 2
# 3
# 4
printem [[1],[2],[3],[4],[5],[6]], 7
# 1
# 2
# 3
# 4
# 5
# 6

Ruby Script to Create an Array of Arrays

I have written a simple screen scraping script and at the end of the script I am attempting to create an array of arrays in preparation for an activerecord insert. The structure I am trying to achieve is as follows:
Array b holds a series of 10 element arrays
b = [[0,1,2,3,4,5,6,7,8,9],[0,1,2,3,4,5,6,7,8,9],[0,1,2,3,4,5,6,7,8,9]]
Currently when I try to print out Array b the array is empty. I'm still fairly new to ruby and programming for that matter and would appreciate any feedback on how to get values in array b and to improve the overall script. Script follows:
require "rubygems"
require "celerity"
t = 0
r = 0
c = 0
a = Array.new(10)
b = Array.new
#initialize Browser
browser = Celerity::IE.new
#goto Login Page
browser.goto('http://www1.drf.com/drfLogin.do?type=membership')
#input UserId and Password
browser.text_field(:name, 'p_full_name').value = 'username'
browser.text_field(:name, 'p_password').value = 'password'
browser.button(:index, 2).click
#goto DRF Frontpage
browser.goto('http://www.drf.com/frontpage')
#goto DRF Entries
browser.goto('http://www1.drf.com/static/indexMenus/eindex.html')
#click the link to access the entries
browser.link(:text, '09').click
browser.tables.each do |table|
t = t + 1
browser.table(:index, t).rows.each do |row|
r = r + 1
browser.table(:index, t).row(:index, r).cells.each do |cell|
a << cell.text
end
b << a
a.clear
end
r = 0
end
puts b
browser.close
This a minor rewrite of your main loop to a more Ruby-like way.
b = Array.new
browser.tables.each_with_index do |table, t|
browser.table(:index, 1 + t).rows.each_with_index do |row, r|
a = Array.new(10)
browser.table(:index, 1 + t).row(:index, 1 + r).cells.each do |cell|
a << cell.text
end
b << a
end
end
puts b
I moved the array initializations to immediately above where they'll be needed. That's a programmer-choice thing of course.
Rather than create two counter variables up above, I switched to using each_with_index which adds an index variable, starting at 0. To get your 1-offsets I add 1.
They're not big changes but they add up to a more cohesive app.
Back to the original code: One issue I see with it is that you create your a array outside the loops then reuse it when you assign to b. That means that each time the same array gets used, but cleared and values stored to it. That will cause the previous array values to be overwritten, but resulting in duplicated arrays in b.
require 'pp'
a = []
b = []
puts a.object_id
a[0] = 1
b << a
a.clear
a[0] = 2
b << a
puts
pp b
b.each { |ary| puts ary.object_id }
# >> 2151839900
# >>
# >> [[2], [2]]
# >> 2151839900
# >> 2151839900
Notice that the a array gets reused repeatedly.
If I change a to a second array there are two values for b and a is two separate objects:
require 'pp'
a = []
b = []
puts a.object_id
a[0] = 1
b << a
a = []
a[0] = 2
b << a
puts
pp b
b.each { |ary| puts ary.object_id }
# >> 2151839920
# >>
# >> [[1], [2]]
# >> 2151839920
# >> 2151839780
Hopefully that'll help you avoid the problem in the future.
Your problem is there at the end:
b << a # push a *reference to* a onto b
a.clear # clear a; the reference in b now points to an empty array!
If you remove the reference to a.clear and start that loop with:
browser.tables.each do |table|
t = t + 1
a = []
...you'll be golden (at least as far as your array-building goes)
I can't tell from your question whether you have multiple tables or not. Maybe just one? In which case:
b = browser.tables.first.rows.map {|row| row.cells.map(&:text)}
If you have multiple tables, and really want an array (tables) of arrays (rows) of arrays (cells), that would be
b = browser.tables.map {|t| t.rows.map {|row| row.cells.map(&:text)}}
And if the tables all have the same structure and you just want all the rows as if they were in one big table, you can do:
b = browser.tables.map {|t| t.rows.map {|row| row.cells.map(&:text)}}.flatten(1)

Resources