Different actions on different lines of a string in RUBY [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following string:
aaaaaa; bbbbbbbb
cccccc; cccccccc
dddddd; dddddddd
I need to take different action in respect to different lines.
More precisely, I must do something-1 with the first line, something-2 with all the other, and something-3 with the last one. Pseudo code:
out.each_line { |ln|
if ln first
do somenthing-1
end
if ln others
do somenthing-2
end
if ln last
do somenthing-3
end
}

I find this alternative a bit more readable:
first, *middle, last = out.lines
do_something_1(first)
middle.each{|line| do_something_2(line) }
do_something_3(last)

How about:
lines = out.lines
do_something_1(lines.first)
lines[1..-2].each do |line|
do_something_2(line)
end
do_something_3(lines.last)
This code will do some action on the first line, some other action on all lines except the first and the last, and a third action on the last line.

Related

Anyone can comment this ruby code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm a total novice in ruby, i came across this code in an article about a bug in gmail:
(0..0xFFFFFFFFFF).each do |i|
puts "#{"%010X" % i}"
end
it is supposed to generate an dictionary, but i can't figure out how it works
Thank You all!
The code iterates and prints all values from 0 to 0xFFFFFFFFFF Similar to how
(1..10).each do |i|
puts i
end
iterates and prints all values from 1 to 10.
For each value between 0 and 0xFFFFFFFFFF it simply prints out its current hex value:
0000000000
...
0000005E6A
0000005E6B
0000005E6C
0000005E6D
0000005E6E
0000005E6F
...
FFFFFFFFFF

Regex for extracting word-number pairs with simple separators [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to find a regex that does the following. Let's say I have a string in this form
wordcount = "THE:12 IT:3 TO:3".
which is a word and its frequency. I need a regex that can find for example THe, followed by :, followed by a number.
If you want all matches use the scan method:
mystring.scan(/\w+:\d+/)
Bonus if you are planning to make a hash:
Hash[mystring.scan(/(\w+):(\d+)/)]
# or, if you prefer to not use regexp:
Hash[x.split.map{|y| y.split(':')}]
You can do as below :
s = "THE:12 IT:3 TO:3"
p s.scan(/\w+:\d+/)
# >> ["THE:12", "IT:3", "TO:3"]

Extract text between V="..." Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Ok instead of printing the whole line:
<von_icd_code V="A00"/>
I only would like to extract the text between V="..", in this case A00
Using Nokogiri::XML::Document
require 'nokogiri'
doc = Nokogiri::XML::Document.parse('<von_icd_code V="A00"/>')
doc.at("von_icd_code")["V"] # => "A00"
scan is the wrong method if you are interested only in a single occurrence. There must also, in general, be a check that the substring was found at all.
The code should look like this
s = '<von_icd_code V="A00"/>'
if s =~ /V="([^"]*)"/
puts $~[1]
end
output
A00
Like this:
'<von_icd_code V="A00"/>'.scan(/V="(.+)"/)[0][0]
=> "A00"

Making a Python3 Variable Using a Counter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have some BASH code that I want to convert to Python3.
INPUTINDEX$INPUTCOUNTER="$INPUT"
Every time a user types an input the counter (code not shown) increases one number. With this code, the user's second input would be in variable INPUTINDEX2. INPUT, of course, is the user's input. The INPUTCOUNTER is a counter that increases each time an input is entered.
The question: How can I do this in Python3.3?
I suggest just appending to a list and the number of entries in the list is your counter.
#!/usr/bin/env python
def main():
entries=[]
while True:
entries.append(raw_input('input something: '))
if not len(entries[-1]):
entries=entries[:-1]
break
counter=len(entries)
print 'counter %d, entries %s.' % (counter, entries)
if (__name__ == "__main__"):
main()

How to compute the number of char, line by line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a Ruby program that, given a file as parameter, returns a hash or array that gives the number of characters for each line.
How can I do this elegantly in Ruby ?
File.open('file_name').map(&:length)
Check this:
File.open('file_name').inject([]) do |counts, line|
counts << line.size
end
Take a note that it will also count new line characters.
For file with content
aa
aaaa
a
the result will be
[3, 5, 1]
If you don't want to count them, check this method String#chomp

Resources