[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: scanning strings, backward?

Cameron, Gemma (UK)

10/11/2006 12:10:00 PM


Or use /$/ to look for what a line ends in?


Gem

-----Original Message-----
From: Vidar Hokstad [mailto:vidar@edgeio.com]
Sent: 11 October 2006 12:35
To: ruby-talk ML
Subject: Re: scanning strings, backward?

*** WARNING ***

This mail has originated outside your organization, either from an
external partner or the Global Internet.
Keep this in mind if you answer this message.


Bil Kleb wrote:
> So, the tasks are roughly,
>
> 1) Goto line 1707 of the file
> 2) Search backward until line.match /\Wdt\W/i
> 3) Remove /dt/i
>
> I'm stuck on an elegant, i.e., Ruby, way to do the first two steps.
> Please point me toward the glittering light.

For 1) Assuming the lines aren't fixed length: Read in the entire file
into an array (using IO#readlines), in which case you can skip straight
to the line you want, or counting line by line until you find the one
you want.

For 2) Any particular reason why you need to search backwards? My
suggestion would be to look at String#gsub - that will help you handle
both 2) and 3) in one go.

Vidar






********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************