[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to substitute a string with different size

Li Chen

7/31/2007 1:26:00 PM

Hi all,

I deal with a mixture of text and binary file. I want to search a
pattern in the string and replace it with a new pattern, then write to a
new file. I use the following script(only part of the script)

str.gsub!(/old_pattern/,"new_pattern")
file.seek(-str.length,IO::SEEK_CUR)
file.print str

It works fine if the both old_pattern and new_pattern are the same size.
But it will produce unpreditable result if the sizes are different. I
wonder if any one out there knows how to explain it and provide a Ruby
way solution.

Thanks,

Li
--
Posted via http://www.ruby-....

3 Answers

Li Chen

7/31/2007 4:10:00 PM

0

Yang Bob wrote:
> Hi Li,
>
> What do you mean 'unpreditable result'? Do you mean that the size of
> 'str'
> object is not correct?
>
>
> 2007/7/31, Li Chen <chen_li3@yahoo.com>:

Actually what I try to do is to replace one string with another string
in some FACS data files, in which part of them are headers in text
format and the rest of them in binary format. For example replace "CD3"
with "CD4" or "CD45". If it is the first case the new file is of same
size and I can open the new file without losing the image information
but I will lost all the image information if I replace "CD3" with
"CD45". This is what I mean by unpreditable result.

Li
--
Posted via http://www.ruby-....

Tim Becker

7/31/2007 4:38:00 PM

0

> Actually what I try to do is to replace one string with another string
> in some FACS data files, in which part of them are headers in text
> format and the rest of them in binary format. For example replace "CD3"
> with "CD4" or "CD45". If it is the first case the new file is of same
> size and I can open the new file without losing the image information
> but I will lost all the image information if I replace "CD3" with
> "CD45". This is what I mean by unpreditable result.

It sounds like there's some sort of index at the beginning of the file
you're changing. E.g. at the beginning of the file it says: 3 bytes of
data, followed by an image. If you substitute "CD3" for "CD45", you'd
also have to update the index at the beginning to say "4 bytes of
data, followed by an image".

I'm afraid I don't know FACS, but it sure sounds like the nature of
the file format doesn't allow you to just search and replace in the
file without updating some context information as well.
-tim

Li Chen

8/1/2007 6:13:00 PM

0

Tim Becker wrote:

> It sounds like there's some sort of index at the beginning of the file
> you're changing. E.g. at the beginning of the file it says: 3 bytes of
> data, followed by an image. If you substitute "CD3" for "CD45", you'd
> also have to update the index at the beginning to say "4 bytes of
> data, followed by an image".
>
> I'm afraid I don't know FACS, but it sure sounds like the nature of
> the file format doesn't allow you to just search and replace in the
> file without updating some context information as well.
> -tim

I don't know FACS either but I ask the technical services and they say
it is a kind of meta data file. If I insert extra characters into the
file I have to remove equal extra spaces from somewhere else in the
file.

Li

--
Posted via http://www.ruby-....