# Create a text file with data custom positioned?

**URL:** <https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994>\
**Category:** Ruby\
**Created:** [April 20, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994 "2009-04-20T20:35:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 20, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994/1 "2009-04-20T20:35:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 20, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994/2 "2009-04-20T21:25:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 20, 2009, 10:55pm UTC](https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994/3 "2009-04-20T22:55:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 21, 2009, 2:47am UTC](https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994/4 "2009-04-21T02:47:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 21, 2009, 8:54pm UTC](https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994/5 "2009-04-21T20:54:31Z")

</div>

Thanks everyone! 🙂 MC

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 21, 2009, 9:41am UTC](https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994/6 "2009-04-21T09:41:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 22, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/create-a-text-file-with-data-custom-positioned/165994/7 "2009-04-22T19:56:27Z")

</div>

@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
