Newbie needs help with parsing a file

I have a file with a long string in the following format:

/vol/dept115345K111455K/vol/home1156668K507768K…ad finitum.

How do I iterate through the string comparing dividing the second number
by the first number and only print the output if the number is 90% or
more? Basically the first number is total space and the second number
is used space. If the comparison is 90% or more, then print the vol
name /vol/dept(etc.) along with the %. If the comparison is less than
90% then don’t print it at all. In other words using the two above I
would get

/vol/dept 96.6%

I am sure this is easy but I can’t get my head around it.

TIA,
JR

On Wed, 4 Jul 2012 07:52:16 +0900
“Jon R.” [email protected] wrote:

words using the two above I would get

/vol/dept 96.6%

str = “/vol/dept115345K111455K/vol/home1156668K507768K”

str[1…-1].split(“/”).inject([]) do |arr, str|
next arr if “vol” == str
next arr if 2 > arr.push(str).count

dept, home = arr
puts “#{dept} VS #{home}”

[]
end

I guess the idea is clear to build final solution :))


Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: [email protected]

*Origin: Happy Hacking!

On Jul 3, 2012, at 16:54 , Aleksey V Zapparov wrote:

second number is used space. If the comparison is 90% or more, then
next arr if “vol” == str
next arr if 2 > arr.push(str).count

dept, home = arr
puts “#{dept} VS #{home}”

[]
end

There are many tools in the String toolbox. #scan is often a much more
powerful tool than #split:

str = “/vol/dept115345K111455K/vol/home1156668K507768K”

str.scan(%r%(/[a-z]+/[a-z]+)(\d+)K(\d+)K%).each do |(vol, quota,
actual)|
pct = actual.to_f / quota.to_f

next unless pct > 0.9

puts “%s %.1f%%” % [vol, pct * 100]
end

Ryan this worked beautifully. Thank you. However, I can’t seem to get
why all of the % signs are in the line puts “%s %.1f%%” % [vol, pct *
100] and what does %s mean?

Ryan D. wrote in post #1067285:

On Jul 3, 2012, at 16:54 , Aleksey V Zapparov wrote:

second number is used space. If the comparison is 90% or more, then
next arr if “vol” == str
next arr if 2 > arr.push(str).count

dept, home = arr
puts “#{dept} VS #{home}”

[]
end

There are many tools in the String toolbox. #scan is often a much more
powerful tool than #split:

str = “/vol/dept115345K111455K/vol/home1156668K507768K”

str.scan(%r%(/[a-z]+/[a-z]+)(\d+)K(\d+)K%).each do |(vol, quota,
actual)|
pct = actual.to_f / quota.to_f

next unless pct > 0.9

puts “%s %.1f%%” % [vol, pct * 100]
end

On Wed, 4 Jul 2012 09:21:04 +0900
Ryan D. [email protected] wrote:

How do I iterate through the string comparing dividing the second
str = “/vol/dept115345K111455K/vol/home1156668K507768K”

puts “%s %.1f%%” % [vol, pct * 100]
end

Oh, man! You’re absolutely right. I guess I need to avoid next time
writing to ML after 12 hours of work ;))


Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: [email protected]

*Origin: Happy Hacking!

On Thu, Jul 5, 2012 at 1:58 PM, Jon R. [email protected] wrote:

Ryan this worked beautifully. Thank you. However, I can’t seem to get
why all of the % signs are in the line puts “%s %.1f%%” % [vol, pct *
100] and what does %s mean?

They are placeholders. Check

“this is a string: %s and this is a number: %d” % [“test”, 27]
=> “this is a string: test and this is a number: 27”

Jesus.