Create a text file with data custom positioned?

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

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.

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

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

Thanks everyone! :slight_smile: MC

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

@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