[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

scanning strings, backward?

Bil Kleb

10/11/2006 11:00:00 AM

Hi,

I would like to automate a code cleanup task which involves
finding warning messages like,

Warning: ../../LibF90/bc_inviscid.f90, line 1707:
DT explicitly imported into BC_INVISCID_FLUX but not used
detected at BC_INVISCID_FLUX@<end-of-statement>

and removing the offending unused import.

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.

Thanks,
--
Bil Kleb
http://fun3d.lar...
16 Answers

vidar

10/11/2006 11:31:00 AM

0


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

M. Edward (Ed) Borasky

10/11/2006 12:45:00 PM

0

Vidar Hokstad wrote:
> 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
>
>
>
For 2, can't you reverse the line and search forwards?

M. Edward (Ed) Borasky

10/11/2006 12:50:00 PM

0

Bil Kleb wrote:
> Hi,
>
> I would like to automate a code cleanup task which involves
> finding warning messages like,
>
> Warning: ../../LibF90/bc_inviscid.f90, line 1707:
> DT explicitly imported into BC_INVISCID_FLUX but not used
> detected at BC_INVISCID_FLUX@<end-of-statement>
>
> and removing the offending unused import.
>
> 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.
>
> Thanks,
> --
> Bil Kleb
> http://fun3d.lar...

Wait a minute ... is the file you're editing always a Fortran source?
Why not just comment out the offending line rather than removing it?
Otherwise, removing a line changes the position number of every line
following it.
>
>


James Gray

10/11/2006 12:54:00 PM

0

On Oct 11, 2006, at 6:05 AM, Bil Kleb wrote:

> So, the tasks are roughly,
>
> 1) Goto line 1707 of the file
> 2) Search backward until line.match /\Wdt\W/i

Would my File::ReadBackwards port help here?

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

I've needed it twice myself lately, so I'm happy to gemify it, if it
will help you.

James Edward Gray II

Bil Kleb

10/11/2006 1:25:00 PM

0

M. Edward (Ed) Borasky wrote:
> Bil Kleb wrote:
>> Warning: ../../LibF90/bc_inviscid.f90, line 1707:
>> DT explicitly imported into BC_INVISCID_FLUX but not used
>> detected at BC_INVISCID_FLUX@<end-of-statement>
>
> Wait a minute ... is the file you're editing always a Fortran source?

Yes.

> Why not just comment out the offending line rather than removing it?

Actually I was just going to remove the offending piece
from the line, e.g.,

use timestep, only : dt, dtau, n_time

would become,

use timestep, only : dtau, n_time

if 'dt' was the unused import.

Regards,
--
Bil Kleb
http://fun3d.lar...

Bil Kleb

10/11/2006 1:27:00 PM

0

James Edward Gray II wrote:
>
> Would my File::ReadBackwards port help here?
>
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

Maybe, but I'll wait and see what else comes up first...

Thanks though,
--
Bil Kleb
http://fun3d.lar...

Bil Kleb

10/11/2006 1:42:00 PM

0

Vidar Hokstad wrote:
> Bil Kleb wrote:
>> 1) Goto line 1707 of the file
>> 2) Search backward until line.match /\Wdt\W/i
>> 3) Remove /dt/i
>
> 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.

The problem is I only want to replace only
the first occurrence before line 1707.

But, by reversing the lines per Ed B's suggestion,
I should be able to use String#sub on a temporary
buffer, and glue things back together for the
next item / line number combination? (not sounding
terribly elegant)

Later,
--
Bil Kleb
http://fun3d.lar...

Morton Goldberg

10/11/2006 2:17:00 PM

0

On Oct 11, 2006, at 7:35 AM, Vidar Hokstad wrote:

>
> 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.

I like this idea, but I would suggest using a hash rather than an
array. Hash the lines keyed to their line number. Then you can search
any which way you like.

Regards, Morton



vidar

10/11/2006 2:21:00 PM

0


Morton Goldberg wrote:
> I like this idea, but I would suggest using a hash rather than an
> array. Hash the lines keyed to their line number. Then you can search
> any which way you like.

I don't see what this would give you. An array of the lines in the
order read would be keyed to the line number. Assuming the Ruby array
implementation is sane using an array should be faster, and also has
the advantage that IO#readlines does all the work of reading the lines
in from file and returning a ready populated array.

Vidar

Bil Kleb

10/11/2006 2:27:00 PM

0

Bil Kleb wrote:
>
> So, the tasks are roughly,

I should have just posted a test? Warning: untested test ;)

require 'import_cleaner'
require 'test/unit'

def class TestImportCleaner < Test::Unit::TestCase
def test_simple_case
compiler_output =<<-EOS
Warning: code.f90, line 8:
DT explicitly imported into SAY_TIME but not used
detected at SAY_TIME@<end-of-statement>
EOS
fortran_code =<<-EOC
subroutine print_dt
use timestep, only: dt
print*, dt
end
subroutine say_time
use timestep, only: dt, time
print *, 'Time: ', time
end
EOC
clean_code = fortran_code.clean_imports(compiler_output)
assert_match /only: time$/, clean_code, 'did not remove extraneous import'
assert_match /only: dt$/, clean_code, 'removed wrong import'
assert_match /print\*, dt/, clean_code, 'modified other code'
end
end

Regards,
--
Bil Kleb
http://fun3d.lar...