# Newbie qustion

**URL:** <https://www.ruby-forum.com/t/newbie-qustion/100943>\
**Category:** Ruby\
**Created:** [August 2, 2006, 12:35am UTC](https://www.ruby-forum.com/t/newbie-qustion/100943 "2006-08-02T00:35:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 2, 2006, 12:35am UTC](https://www.ruby-forum.com/t/newbie-qustion/100943/1 "2006-08-02T00:35:06Z")

</div>

I’m trying to parse a file that looks like this:

text…  
text…  
numbers…

IDS\_SOMETHIGN\_SOMETHING “giving you cookies in a sec”  
IDX\_SOMETHING\_SOMETHIGN “hi again”  
IDY\_SMOTHIENG\_SOMETHIGN\_SOMTHIGN “HELO world”  
ID\_ONE\_THING “hello how are you”  
ID\_ONETHINGTOO “give me a number”  
ID\_ONEMORE\_THING\_ANDMORE “this are strings”

I’m trying to create a hash table but i’m not sure what’s wrong with my  
delimeters:

class Parser

table = { }  
IO.foreach(‘resurcefiletest’) { |line|  
if line =~ /^ID\* (._?) = \s_ " (._?) "/x  
 #if line =~ /^ \s_ " (._?) " \s_ = \s\* " (.\*?) "/x  
table[$1] = $2  
end  
}

#puts table[“StaplingTitle”]  
p table

end

can somebody give me a hand?

thanks,  
mike

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 2, 2006, 12:46am UTC](https://www.ruby-forum.com/t/newbie-qustion/100943/2 "2006-08-02T00:46:53Z")

</div>

On Aug 1, 2006, at 3:35 PM, Robert S. wrote:

> ID\_ONETHINGTOO “give me a number”  
> if line =~ /^ID\* (._?) = \s_ " (._?) "/x  
> #if line =~ /^ \s_ " (._?) " \s_ = \s\* " (.\*?) "/x  
> table[$1] = $2  
> end  
> }
> 
> #puts table[“StaplingTitle”]  
> p table  
> end

//x just gets confusing. Make your regexp simple:

table = {}

File.foreach filename do |line|  
next unless line =~ /^ID(\S+) “(.\*?)”/  
table[$1] = $2  
end

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 2, 2006, 1:53am UTC](https://www.ruby-forum.com/t/newbie-qustion/100943/3 "2006-08-02T01:53:48Z")

</div>

Eric H. wrote:

> On Aug 1, 2006, at 3:35 PM, Robert S. wrote:
> 
> > ID\_ONETHINGTOO “give me a number”  
> > if line =~ /^ID\* (._?) = \s_ " (._?) "/x  
> > #if line =~ /^ \s_ " (._?) " \s_ = \s\* " (.\*?) "/x  
> > table[$1] = $2  
> > end  
> > }
> > 
> > #puts table[“StaplingTitle”]  
> > p table  
> > end
> 
> //x just gets confusing. Make your regexp simple:
> 
> table = {}
> 
> File.foreach filename do |line|  
> next unless line =~ /^ID(\S+) “(.\*?)”/  
> table[$1] = $2  
> end

Thanks for your help eric,

I tried what you said but nothing gets stored in the table…and i’m  
nothing seeing why. do you see it?

thanks

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 4, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/newbie-qustion/100943/4 "2006-08-04T19:15:45Z")

</div>

On Aug 1, 2006, at 4:54 PM, Robert S. wrote:

> Eric H. wrote:
> 
> > On Aug 1, 2006, at 3:35 PM, Robert S. wrote:
> > 
> > > if line =~ /^ID\* (._?) = \s_ " (.\*?) "/x

> > next unless line =~ /^ID(\S+) “(.\*?)”/
> 
> I tried what you said but nothing gets stored in the table…and i’m  
> nothing seeing why. do you see it?

change the space to \s+

your original regexp didn’t work because there is no =, but I agree  
with eric that your use of /x makes it too complex.

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 3, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/newbie-qustion/100943/5 "2006-08-03T12:08:09Z")

</div>

Robert S. wrote:

> ID\_ONETHINGTOO “give me a number”  
> #if line =~ /^ \s\* " (._?) " \s_ = \s\* " (.\*?) "/x  
> can somebody give me a hand?  
> ---- code: ----  
> table = {}

DATA.each { |line|  
if line =~ /^ID ( \S+ ) \s+ " (.\*) "/x  
table[$1] = $2  
end  
}  
puts table.to\_a.map{|a| a.join " =\> " }

**END**  
text…  
text…  
numbers…

IDS\_SOMETHIGN\_SOMETHING “giving you cookies in a sec”  
IDX\_SOMETHING\_SOMETHIGN “hi again”  
IDY\_SMOTHIENG\_SOMETHIGN\_SOMTHIGN “HELO world”  
ID\_ONE\_THING “hello how are you”  
ID\_ONETHINGTOO “give me a number”  
ID\_ONEMORE\_THING\_ANDMORE “this are strings”

---- output: ----  
\_ONE\_THING =\> hello how are you  
\_ONETHINGTOO =\> give me a number  
Y\_SMOTHIENG\_SOMETHIGN\_SOMTHIGN =\> HELO world  
S\_SOMETHIGN\_SOMETHING =\> giving you cookies in a sec  
\_ONEMORE\_THING\_ANDMORE =\> this are strings  
X\_SOMETHING\_SOMETHIGN =\> hi again
