Forum: Ruby create a text file with data custom positioned?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2009-04-20 20:35
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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-04-20 21:25
(Received via mailing list)
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.
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2009-04-20 22:55
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
6e191124d011c102f295ae1bccf20ee1?d=identicon&s=25 Robert Schaaf (Guest)
on 2009-04-21 02:47
(Received via mailing list)
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
64aa4b69fdd7127e6f3ee16ae065a620?d=identicon&s=25 Giampiero Zanchi (giampiz)
on 2009-04-21 09:41
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
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2009-04-21 20:54
Thanks everyone! :-) MC
Ac816d6a1c7a649d7aafed6afd05f64f?d=identicon&s=25 Alan Claughan (Guest)
on 2009-04-22 19:56
(Received via mailing list)
@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
This topic is locked and can not be replied to.