From: Aleksey V Zapparov Date: 2012-07-04T17:57:00+09:00 Subject: Re: Newbie needs help with parsing a file --Sig_/t.05/jz65xTV7pdec7rg1UL Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable On Wed, 4 Jul 2012 09:21:04 +0900 Ryan Davis wrote: >=20 > On Jul 3, 2012, at 16:54 , Aleksey V Zapparov wrote: >=20 > > On Wed, 4 Jul 2012 07:52:16 +0900 > > "Jon R." wrote: > >=20 > >> I have a file with a long string in the following format: > >>=20 > >> /vol/dept115345K111455K/vol/home1156668K507768K...ad finitum. > >>=20 > >> 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 > >>=20 > >> /vol/dept 96.6% > >=20 > >=20 > > str =3D "/vol/dept115345K111455K/vol/home1156668K507768K" > >=20 > > str[1..-1].split("/").inject([]) do |arr, str| > > next arr if "vol" =3D=3D str > > next arr if 2 > arr.push(str).count > >=20 > > dept, home =3D arr > > puts "#{dept} VS #{home}" > >=20 > > [] > > end >=20 > There are many tools in the String toolbox. #scan is often a much > more powerful tool than #split: >=20 > str =3D "/vol/dept115345K111455K/vol/home1156668K507768K" >=20 > str.scan(%r%(/[a-z]+/[a-z]+)(\d+)K(\d+)K%).each do |(vol, quota, > actual)| pct =3D actual.to_f / quota.to_f >=20 > next unless pct > 0.9 >=20 > puts "%s %.1f%%" % [vol, pct * 100] > end >=20 >=20 Oh, man! You're absolutely right. I guess I need to avoid next time writing to ML after 12 hours of work ;)) --=20 Sincerely yours, Aleksey V. Zapparov A.K.A. ixti FSF Member #7118 Mobile Phone: +34 677 990 688 Homepage: http://www.ixti.net JID: zapparov@jabber.ru *Origin: Happy Hacking! --Sig_/t.05/jz65xTV7pdec7rg1UL Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iQEcBAEBAgAGBQJP9AVUAAoJEGRO9nGLm9Y8zbQIAIMTZcxxhbpljZZ9r4dxWm5+ G65A+zsEu4bXwyrhL8C28HekwXl/LYnxa7wReq0XlkEvcbB6u1pPLqxb9e0mme/D A9mb/3UuEk4yXnJfjXOvrh23K4iy73v/hF+CkXxmnApYLLSI4RmAB8PiKZcw9H/T /bgsl7ahk/TWxge1fMzgXkc5l3CcEmk4WlI+y8itFNgeushEHj7DgL7Fof0spFtw McNygcuYqDusx2EA08Q4bGfs7aosvekk+Rz4C6He2OnXYktBI1tsvV29iAFDK70d iz1RI4leKhFAFdYoyPIn4j/gqqmyeWJMph41JgW76aDZ/tC6+RKBHfhowtq55wg= =rgKN -----END PGP SIGNATURE----- --Sig_/t.05/jz65xTV7pdec7rg1UL--