[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

create a text file with data custom positioned?

Mmcolli00 Mom

4/20/2009 6:36:00 PM

Do you know how to create a text file with data custom positioned?
Thanks MC

Each column starts on a different position
column1 position(0)
column2 position(13)
column3 position(20)
column4 position(28)

This is an example of the way the text file should look:

SL_ewer00 45 SL_PRO1 4534 0
SL_erer 46 SL_PRO2344 46 0
SL_2344 47 SL_PRO334 47 0

#*********SNIPPPET

doc.elements.each("*//FieldMap") do |element|
@fromField = element.attributes["FromFieldID"]
@toField = element.attributes["ToFieldID"]
@fromIndex = element.attributes["FromIndex"]
@toIndex = element.attributes["ToIndex"]

File.open('C:\dRar\spn.txt', 'r+') do |f1|
f1.position 0
f1.puts @fromField
f1.seek 13
f1.puts "@toField
f1.seek 20
f1.puts @fromIndex
f1.seek 27
f1.puts @fromIndex
end
end
--
Posted via http://www.ruby-....

6 Answers

Phlip

4/20/2009 7:20:00 PM

0

Mmcolli00 Mom wrote:

> column1 position(0)
> column2 position(13)
> column3 position(20)
> column4 position(28)
>
> This is an example of the way the text file should look:

El Goog will lead you to sprintf. Then look at the %s pattern, such as
%0.13s.


Mmcolli00 Mom

4/20/2009 8:56:00 PM

0


sprintf didn't work, it did position them however it positioned them
according to my last data element in that line so the columns were not
straight.

f1.puts sprintf("%0s", @SPNHash.values_at(@fromField)+sprintf("%13s",
@fromIndex)+sprintf("%20s",
@SPNHash.values_at(@toField).to_s)+sprintf("%28s", @toIndex)

output example:

Aerwrerereres 0 sfendki0098 0
REFED 0 Derdtd 12
d34dData 0 dedDate 0
--
Posted via http://www.ruby-....

Robert Schaaf

4/21/2009 12:47:00 AM

0

Your example doesn't match either the number of args (5) or their order.

Assuming that you want two field name/index pairs, as suggested by
your output, I'd use

somefile,printf("%-12s%-8d%-12s%-8d0\n", @fromField, @fromIndex,
@toField, @toIndex)

Fun stuff,

Bob Schaaf


On Apr 20, 2009, at 2:35 PM, Mmcolli00 Mom wrote:

> Do you know how to create a text file with data custom positioned?
> Thanks MC
>
> Each column starts on a different position
> column1 position(0)
> column2 position(13)
> column3 position(20)
> column4 position(28)
>
> This is an example of the way the text file should look:
>
> SL_ewer00 45 SL_PRO1 4534 0
> SL_erer 46 SL_PRO2344 46 0
> SL_2344 47 SL_PRO334 47 0
>
> #*********SNIPPPET
>
> doc.elements.each("*//FieldMap") do |element|
> @fromField = element.attributes["FromFieldID"]
> @toField = element.attributes["ToFieldID"]
> @fromIndex = element.attributes["FromIndex"]
> @toIndex = element.attributes["ToIndex"]
>
> File.open('C:\dRar\spn.txt', 'r+') do |f1|
> f1.position 0
> f1.puts @fromField
> f1.seek 13
> f1.puts "@toField
> f1.seek 20
> f1.puts @fromIndex
> f1.seek 27
> f1.puts @fromIndex
> end
> end
> --
> Posted via http://www.ruby-....
>


Giampiero Zanchi

4/21/2009 7:42:00 AM

0

I have a function; it receives a string, a char with possible values l
or r (left or right alignment), the desired length and a filler char

def just(a_string,lr,a_len,a_fil)
if lr == 'l' then
a_string = a_string.ljust(a_len,a_fil)
else
a_string = a_string.rjust(a_len,a_fil)
end
end

then I use it as follows (with arrays of strings, alignment marks and
lengths):

just(cols[i],aligns[i],col_len[i]," ")

for instance

just("My_string","l",12,".")

produces

"My_string..."

hope this helps

ciao

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

Mmcolli00 Mom

4/21/2009 6:55:00 PM

0

Thanks everyone! :-) MC
--
Posted via http://www.ruby-....

Alan Claughan

4/22/2009 5:56:00 PM

0

@col1 = 'SL_ewer00'
@col2 = 45
@col3 = 'SL_PRO1'
@col4 = 4534
@col5 = 0

outfile = File.open('spn.txt', 'w')
outfile.puts "%12s" % @col1 + "%12d" % @col2 + "%12s" % @col3 + "%12d"
% @col4 + "%12d" % @col5
outfile.close

=begin
produces
SL_ewer00 45 SL_PRO1 4534 0

=end

On 20 Apr 2009, at 8:35 PM, Mmcolli00 Mom wrote:

> Do you know how to create a text file with data custom positioned?
> Thanks MC
>
> Each column starts on a different position
> column1 position(0)
> column2 position(13)
> column3 position(20)
> column4 position(28)
>
> This is an example of the way the text file should look:
>
> SL_ewer00 45 SL_PRO1 4534 0
> SL_erer 46 SL_PRO2344 46 0
> SL_2344 47 SL_PRO334 47 0
>
> #*********SNIPPPET
>
> doc.elements.each("*//FieldMap") do |element|
> @fromField = element.attributes["FromFieldID"]
> @toField = element.attributes["ToFieldID"]
> @fromIndex = element.attributes["FromIndex"]
> @toIndex = element.attributes["ToIndex"]
>
> File.open('C:\dRar\spn.txt', 'r+') do |f1|
> f1.position 0
> f1.puts @fromField
> f1.seek 13
> f1.puts "@toField
> f1.seek 20
> f1.puts @fromIndex
> f1.seek 27
> f1.puts @fromIndex
> end
> end
> --
> Posted via http://www.ruby-....
>
>
> __________________________________________________
> D O T E A S Y - "Join the web hosting revolution!"
> http://www.d...