# Add to words syntaxes

**URL:** <https://www.ruby-forum.com/t/add-to-words-syntaxes/82470>\
**Category:** Ruby\
**Created:** [March 16, 2006, 8:28am UTC](https://www.ruby-forum.com/t/add-to-words-syntaxes/82470 "2006-03-16T08:28:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@dani](https://www.ruby-forum.com/u/dani)\
**Post date:** [March 16, 2006, 8:28am UTC](https://www.ruby-forum.com/t/add-to-words-syntaxes/82470/1 "2006-03-16T08:28:05Z")

</div>

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:  
Teszt Kft  
33445566222  
  
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.  
) begin from front, so long it has no more escape characters.  
So, how can I do this? Please if you can help me…

Daniel

---

<div class="post-metadata">

**Author:** ![dani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@dani](https://www.ruby-forum.com/u/dani)\
**Post date:** [March 16, 2006, 9:44am UTC](https://www.ruby-forum.com/t/add-to-words-syntaxes/82470/2 "2006-03-16T09:44:06Z")

</div>

On Thu, 2006-03-16 at 16:25 +0900, Dani wrote:

> I need that ruby add to each line several XML tags. So lets say:  
> Teszt Kft  
> 33445566222  
>   
> 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.  
> ) 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  
(or whatever number)? Or is it single-line, with some way to choose  
which tag goes on a given line?

- What do you mean ‘use another script’ for the two-column  
lines? I note as well that it could be difficult to determine  
which lines are two column, since spaces seem to be allowed  
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(/^(.\*)$/) { “#$1” }

# =\> "Teszt Kft

# 33445566222

# ;

# 20060101

# 20060131

# 0A0001C002A 33445566222

# 0A0001C007A Teszt kft"

s.map { |line| “#{line.chomp}\n” }.join

# =\> as above

s.map do |line|  
tag = (line =~ /^\d+$/) ? ‘foo’ : ‘bar’  
“\<#{tag}\>#{line.chomp}\</#{tag}\>\n”  
end.join

# =\> "Teszt Kft

```
  <foo>33445566222</foo>
  <bar>;</bar>
  <foo>20060101</foo>
  <foo>20060131</foo>
  <bar>0A0001C002A 33445566222</bar>
  <bar>0A0001C007A Teszt kft</bar>"
```
