# Add to words syntaxes

**URL:** <https://rubytalk.org/t/add-to-words-syntaxes/25814>\
**Category:** ruby-talk\
**Created:** [16 March 2006 09:14 UTC](https://rubytalk.org/t/add-to-words-syntaxes/25814 "2006-03-16T09:14:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dani](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Dani](https://rubytalk.org/u/Dani)\
**Post date:** [16 March 2006 09:14 UTC](https://rubytalk.org/t/add-to-words-syntaxes/25814/1 "2006-03-16T09:14:15Z")

</div>

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\>  
&nbsp;&nbsp;\<nyomtatvany\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<nyomtatvanyinformacio\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<nyomtatvanyazonosito\>0608A\</nyomtatvanyazonosito\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<adozo\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<nev\>Teszt kft\</nev\> # the first line from the txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<adoszam\>33445566222\</adoszam\> # second line  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<adoazonosito\>\</adoazonosito\> #thrid line, in this txt is blank  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</adozo\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<idoszak\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tol\>20060101\</tol\> # fourth  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<ig\>20060131\</ig\> # fifth  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</idoszak\>  
&nbsp;&nbsp;&nbsp;&nbsp;\</nyomtatvanyinformacio\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<mezok\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<mezo eazon="0A0001C002A"\>11111111122\</mezo\> # and the lines...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<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|  
&nbsp;&nbsp;words = line.split  
&nbsp;&nbsp;'\<mezo eazon="' + words[0] + '"\>' + words[1] + '\</mezo\>' + "\n"  
end

File.open(outfile,'w') do |file|  
&nbsp;&nbsp;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

> **···**
>
> -----Original Message-----  
> From: Ross Bamford [mailto:rossrt@roscopeco.co.uk]  
> Sent: Thursday, March 16, 2006 9:42 AM  
> To: ruby-talk ML  
> Subject: Re: add to words syntaxes
> 
> On Thu, 2006-03-16 at 16:25 +0900, Dani wrote:
> 
> > Hi!  
> > Still have a question: I have a txt file with words, seperated with  
> > semicolon or linebreaks. looks like this:  
> > Teszt Kft  
> > 33445566222  
> > ;  
> > 20060101  
> > 20060131  
> > 0A0001C002A 33445566222  
> > 0A0001C007A Teszt kft
> > 
> > I need that ruby add to each line several XML tags. So lets say:  
> > \<hello\>Teszt Kft\</hello\>  
> > \<foo\>33445566222\</foo\>  
> > \<bar\>\</bar\>  
> > and so on... from line 6 (where are two columns) should ruby use  
> > another (ruby)script. And when it reached the end of the section
> 
> (where
> 
> > could be an escape character or something, what it changes to ie.  
> > \</end\>) begin from front, so long it has no more escape characters.  
> > So, how can I do this? Please if you can help me...
> 
> Perhaps it's me not being awake properly, but it doesn't seem very clear  
> from your question exactly what you want to do? To clarify:
> 
> + Are the lines you want to process always in sets of 3  
> &nbsp;&nbsp;(or whatever number)? Or is it single-line, with some way to choose  
> &nbsp;&nbsp;which tag goes on a given line?
> 
> + What do you mean 'use another script' for the two-column  
> &nbsp;&nbsp;lines? I note as well that it could be difficult to determine  
> &nbsp;&nbsp;which lines are two column, since spaces seem to be allowed  
> &nbsp;&nbsp;in the processed input anyway.
> 
> Anyway, I'm sure this isn't what you're after, but maybe it'll point you  
> in the right direction. Failing that, post a bit more detail and sample  
> input/output and I'm sure you'll get what you need.
> 
> s = "\<your sample input, above\>"  
> s.gsub(/^(.\*)$/) { "\<foo\>#$1\</foo\>" }
> 
> # =\> "\<foo\>Teszt Kft\</foo\>  
> # \<foo\>33445566222\</foo\>  
> # \<foo\>;\</foo\>  
> # \<foo\>20060101\</foo\>  
> # \<foo\>20060131\</foo\>  
> # \<foo\>0A0001C002A 33445566222\</foo\>  
> # \<foo\>0A0001C007A Teszt kft\</foo\>"
> 
> s.map { |line| "\<foo\>#{line.chomp}\</foo\>\n" }.join  
> # =\> as above
> 
> s.map do |line|  
> &nbsp;&nbsp;tag = (line =~ /^\d+$/) ? 'foo' : 'bar'  
> &nbsp;&nbsp;"\<#{tag}\>#{line.chomp}\</#{tag}\>\n"  
> end.join  
> # =\> "\<bar\>Teszt Kft\</bar\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<foo\>33445566222\</foo\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<bar\>;\</bar\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<foo\>20060101\</foo\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<foo\>20060131\</foo\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<bar\>0A0001C002A 33445566222\</bar\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<bar\>0A0001C007A Teszt kft\</bar\>"
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Carlos](https://rubytalk.org/u/Carlos)\
**Post date:** [16 March 2006 09:45 UTC](https://rubytalk.org/t/add-to-words-syntaxes/25814/2 "2006-03-16T09:45:51Z")

</div>

Dani wrote:

> 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\>  
> &nbsp;&nbsp;\<nyomtatvany\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\<nyomtatvanyinformacio\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<nyomtatvanyazonosito\>0608A\</nyomtatvanyazonosito\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<adozo\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<nev\>Teszt kft\</nev\> # the first line from the txt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<adoszam\>33445566222\</adoszam\> # second line  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<adoazonosito\>\</adoazonosito\> #thrid line, in this txt is blank  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</adozo\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<idoszak\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tol\>20060101\</tol\> # fourth  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<ig\>20060131\</ig\> # fifth  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</idoszak\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\</nyomtatvanyinformacio\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\<mezok\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<mezo eazon="0A0001C002A"\>11111111122\</mezo\> # and the lines...  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<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:

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

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

> marked\_up\_lines = lines.map do |line|  
> &nbsp;&nbsp;words = line.split  
> &nbsp;&nbsp;'\<mezo eazon="' + words[0] + '"\>' + words[1] + '\</mezo\>' + "\n"  
> end
> 
> File.open(outfile,'w') do |file|

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.write first\_six\_data

> &nbsp;&nbsp;file.write marked\_up\_lines.join  
> end

HTH

(warning: code not tested)
