Forum: Ruby Re: add to words syntaxes

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.
E59545b25529b8ae09caffe860663296?d=identicon&s=25 Dani (Guest)
on 2006-03-16 10:15
(Received via mailing list)
Thanks, for the answers, but didnt worked. I try to clear my problem,
Here is the input:

0608A;
Teszt Kft;
33445566222;
;
20060101;
20060131;
0A0001C002A 33445566222
0A0001C007A Teszt kft
# this text repeat himself with other data several times

This, should look like this:

<?xml version="1.0" encoding="windows-1250"?>
<nyomtatvanyok>
  <nyomtatvany>
    <nyomtatvanyinformacio>
      <nyomtatvanyazonosito>0608A</nyomtatvanyazonosito>
      <adozo>
        <nev>Teszt kft</nev> # the first line from the txt
        <adoszam>33445566222</adoszam> # second line
        <adoazonosito></adoazonosito> #thrid line, in this txt is blank
      </adozo>
      <idoszak>
        <tol>20060101</tol> # fourth
        <ig>20060131</ig> # fifth
      </idoszak>
    </nyomtatvanyinformacio>
    <mezok>
      <mezo eazon="0A0001C002A">11111111122</mezo> # and the lines...
      <mezo eazon="0A0001C007A">Próba Cég</mezo>
.....
</mezok></nyomtatvany></nyomtatvanyok>

So my problem is how can I get an XML source like above. For the columns
I already have this script:
outfile = ARGV.shift

lines = ARGF.readlines
marked_up_lines = lines.map do |line|
  words = line.split
  '<mezo eazon="' + words[0] + '">' + words[1] + '</mezo>' + "\n"
end

File.open(outfile,'w') do |file|
  file.write marked_up_lines.join
end

This works fine, but I dont know what to do with the first 6 lines to
bring it to look like above and how can I bring ruby to repeat himself
and what for a character need I to use for repeating..
Regards,

Daniel
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 Carlos (Guest)
on 2006-03-16 10:49
(Received via mailing list)
Dani wrote:
> 0A0001C007A Teszt kft
>         <nev>Teszt kft</nev> # the first line from the txt
>       <mezo eazon="0A0001C007A">Próba Cég</mezo>
> .....
> </mezok></nyomtatvany></nyomtatvanyok>
>
> So my problem is how can I get an XML source like above. For the columns
> I already have this script:
> outfile = ARGV.shift
>
> lines = ARGF.readlines

Well, before you start to loop to fill the "mezo" tags, extract the
other lines:

  nyomtatvanyazonosito, nev, adoszam, adoazonosito, tol, ig =
    lines.slice!(0,6).map {|w| w.chomp.chomp(';') }
  first_six_data = <<-EOT
<?xml ...
<nyomtatvanyinformacio>
   <nyomtatvanyazonosito>#{nyomtatvanyazonosito}</nyomtatvanyazonosito>
      <adozo>
        <nev>#{nev}</nev>
  ...
EOT

...And then continue processing the array as before.

> marked_up_lines = lines.map do |line|
>   words = line.split
>   '<mezo eazon="' + words[0] + '">' + words[1] + '</mezo>' + "\n"
> end
>
> File.open(outfile,'w') do |file|
     file.write first_six_data
>   file.write marked_up_lines.join
> end

HTH

(warning: code not tested)
This topic is locked and can not be replied to.