# String scanning woes :(

**URL:** https://rubytalk.org/t/string-scanning-woes/44265
**Category:** ruby-talk
**Created:** [5 February 2008 00:54 UTC](https://rubytalk.org/t/string-scanning-woes/44265 "2008-02-05T00:54:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Lovell](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@Lovell](https://rubytalk.org/u/Lovell)
#### Post date: [5 February 2008 00:54 UTC](https://rubytalk.org/t/string-scanning-woes/44265/1 "2008-02-05T00:54:58Z")

</div>

Hello all,

I am trying to get a file into an array using .scan and I can't seem  
to get anything to work properly.

I am reading in a file of email addresses (1 per line) and it all  
seems to come in as 1 long string some how. I am trying to use scan  
to break it up into an array of emails so that I can do some uniq  
checks and validation with other arrays. But I just don't seem to get  
it right.

My code right now is as follows:

emails = File.open("/users/lem/desktop/test/  
POCs\_DNB.txt","r").readlines.map! {|x| x.chomp} # Read in the list  
of emails  
email.scan(/\S+/) # To mach on spaces (I assume). I thought I would  
be matching on new lines  
puts email # To verify

When I did an inspect on the email variable The address appeared as  
such

"foo1@bar.edu\foo2@bar.com\foo3@bar.gov......"

This is my absolute first time working with .scan and regular  
expressions so I have a little bit of a learning curve with this one.

Any help is greatly appreciated.

---

<div class="post-metadata">

### Author: ![Brandon\_Jones](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Brandon\_Jones](https://rubytalk.org/u/Brandon_Jones)
#### Post date: [5 February 2008 01:25 UTC](https://rubytalk.org/t/string-scanning-woes/44265/2 "2008-02-05T01:25:03Z")

</div>

You could try this to make it a bit easier:

File.open("/users/lem/desktop/test/POCs\_DNB.txt", "r").each\_line do |  
line\>  
&nbsp;&nbsp;line.chomp!  
&nbsp;&nbsp;# now you have a single line (sans newline) from your file  
end

# no need to close the file either 😃

> **···**
>
> On Feb 4, 7:50 pm, Vell \<lovell.mcilw...@gmail.com\> wrote:
> 
> > Hello all,
> > 
> > I am trying to get a file into an array using .scan and I can't seem  
> > to get anything to work properly.
> > 
> > I am reading in a file of email addresses (1 per line) and it all  
> > seems to come in as 1 long string some how. I am trying to use scan  
> > to break it up into an array of emails so that I can do some uniq  
> > checks and validation with other arrays. But I just don't seem to get  
> > it right.
> > 
> > My code right now is as follows:
> > 
> > emails = File.open("/users/lem/desktop/test/  
> > POCs\_DNB.txt","r").readlines.map! {|x| x.chomp} # Read in the list  
> > of emails  
> > email.scan(/\S+/) # To mach on spaces (I assume). I thought I would  
> > be matching on new lines  
> > puts email # To verify
> > 
> > When I did an inspect on the email variable The address appeared as  
> > such
> > 
> > "f...@bar.edu\f...@bar.com\f...@bar.gov......"
> > 
> > This is my absolute first time working with .scan and regular  
> > expressions so I have a little bit of a learning curve with this one.
> > 
> > Any help is greatly appreciated.

---

<div class="post-metadata">

### Author: ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)
#### Post date: [5 February 2008 01:46 UTC](https://rubytalk.org/t/string-scanning-woes/44265/3 "2008-02-05T01:46:32Z")

</div>

to avoid doubt, try slowly.

this is a first mod/run of your posted code, eg,

botp@pc4all:~$ cat test.txt  
foo1@bar.edu  
foo2@bar.com  
foo3@bar.gov

botp@pc4all:~$ cat test.rb  
p File.readlines("test.txt").map{|x| x.chomp}

botp@pc4all:~$ ruby test.rb  
["foo1@bar.edu", "foo2@bar.com", "foo3@bar.gov"]

that is just one way. there are many ways if using ruby.

kind regards -botp

> **···**
>
> On Feb 5, 2008 8:54 AM, Vell \<lovell.mcilwain@gmail.com\> wrote:
> 
> > I am trying to get a file into an array using .scan and I can't seem  
> > to get anything to work properly.

---

<div class="post-metadata">

### Author: ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)
#### Post date: [5 February 2008 02:07 UTC](https://rubytalk.org/t/string-scanning-woes/44265/4 "2008-02-05T02:07:36Z")

</div>

Lovell Mcilwain wrote:

> Hello all,
> 
> I am trying to get a file into an array using .scan and I can't seem  
> to get anything to work properly.
> 
> I am reading in a file of email addresses (1 per line) and it all  
> seems to come in as 1 long string some how. I am trying to use scan  
> to break it up into an array of emails so that I can do some uniq  
> checks and validation with other arrays. But I just don't seem to get  
> it right.
> 
> My code right now is as follows:
> 
> emails = File.open("/users/lem/desktop/test/  
> POCs\_DNB.txt","r").readlines.map! {|x| x.chomp}
> 
> email.scan(/\S+/)

scan() returns an array. You don't assign the array to any variable, so  
it is discarded.

> When I did an inspect on the email variable The address appeared as  
> such
> 
> "foo1@bar.edu\foo2@bar.com\foo3@bar.gov......"

Nowhere in the code you posted does a variable named email exist.

> This is my absolute first time working with .scan and regular  
> expressions so I have a little bit of a learning curve with this one.
> 
> Any help is greatly appreciated.

If you expect to get relevant help, you should post a short example  
progrram that demonstrates your problem, i.e. an example program that  
anyone can run and get the same results you do.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)
#### Post date: [5 February 2008 11:44 UTC](https://rubytalk.org/t/string-scanning-woes/44265/5 "2008-02-05T11:44:56Z")

</div>

First you say "emails"; then you say "email".  
This is not code that will run.

Didn't you copy and paste? Don't tell us that you  
retyped the code because you were eager for the chance  
to introduce errors.

p IO.readlines( "data" ).map{|x| x.strip }  
p IO.read( "data" ).split  
p IO.read( "data" ).scan(/\S+/)

> **···**
>
> On Feb 4, 6:50 pm, Vell \<lovell.mcilw...@gmail.com\> wrote:
> 
> > emails = File.open("/users/lem/desktop/test/  
> > POCs\_DNB.txt","r").readlines.map! {|x| x.chomp}  
> > email.scan(/\S+/)

---

<div class="post-metadata">

### Author: ![Lovell](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@Lovell](https://rubytalk.org/u/Lovell)
#### Post date: [5 February 2008 05:39 UTC](https://rubytalk.org/t/string-scanning-woes/44265/6 "2008-02-05T05:39:57Z")

</div>

> Lovell Mcilwain wrote:  
> \> Hello all,
> 
> \> I am trying to get a file into an array using .scan and I can't seem  
> \> to get anything to work properly.
> 
> \> I am reading in a file of email addresses (1 per line) and it all  
> \> seems to come in as 1 long string some how. I am trying to use scan  
> \> to break it up into an array of emails so that I can do some uniq  
> \> checks and validation with other arrays. But I just don't seem to get  
> \> it right.
> 
> \> My code right now is as follows:
> 
> \> emails = File.open("/users/lem/desktop/test/  
> \> POCs\_DNB.txt","r").readlines.map! {|x| x.chomp}
> 
> \> email.scan(/\S+/)
> 
> scan() returns an array. You don't assign the array to any variable, so  
> it is discarded.
> 
> \> When I did an inspect on the email variable The address appeared as  
> \> such
> 
> \> "f...@bar.edu\f...@bar.com\f...@bar.gov......"
> 
> Nowhere in the code you posted does a variable named email exist.

Very first line of my code is what I thought to be a variable...

> \> This is my absolute first time working with .scan and regular  
> \> expressions so I have a little bit of a learning curve with this one.
> 
> \> Any help is greatly appreciated.
> 
> If you expect to get relevant help, you should post a short example  
> progrram that demonstrates your problem, i.e. an example program that  
> anyone can run and get the same results you do.

The code I posted is exactly what I ran aside for giving you  
hundrededs of lines of email. The example is exactly what I ran to  
get the results I posted.

> **···**
>
> On Feb 4, 9:07 pm, 7stud -- \<bbxx789\_0...@yahoo.com\> wrote:
> 
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

### Author: ![Lovell](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@Lovell](https://rubytalk.org/u/Lovell)
#### Post date: [5 February 2008 16:39 UTC](https://rubytalk.org/t/string-scanning-woes/44265/7 "2008-02-05T16:39:57Z")

</div>

> \> emails = File.open("/users/lem/desktop/test/  
> \> POCs\_DNB.txt","r").readlines.map! {|x| x.chomp}  
> \> email.scan(/\S+/)
> 
> First you say "emails"; then you say "email".  
> This is not code that will run.
> 
> Didn't you copy and paste? Don't tell us that you  
> retyped the code because you were eager for the chance  
> to introduce errors.

I'm a beginner, lighten up James.

> **···**
>
> On Feb 5, 6:40 am, William James \<w\_a\_x\_...@yahoo.com\> wrote:
> 
> > On Feb 4, 6:50 pm, Vell \<lovell.mcilw...@gmail.com\> wrote:
> 
> > p IO.readlines( "data" ).map{|x| x.strip }  
> > p IO.read( "data" ).split  
> > p IO.read( "data" ).scan(/\S+/)

---

<div class="post-metadata">

### Author: ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)
#### Post date: [5 February 2008 06:45 UTC](https://rubytalk.org/t/string-scanning-woes/44265/8 "2008-02-05T06:45:18Z")

</div>

Lovell Mcilwain wrote:

> The code I posted is exactly what I ran aside for giving you  
> hundrededs of lines of email. The example is exactly what I ran to  
> get the results I posted.

emails = File.open("data.txt").readlines.map! {|x| x.chomp}  
email.scan(/\S+/)

--output:--  
r1test.rb:2: undefined local variable or method `email' for main:Object  
(NameError)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [5 February 2008 09:52 UTC](https://rubytalk.org/t/string-scanning-woes/44265/9 "2008-02-05T09:52:53Z")

</div>

Though shalt use the block form of File.open to ensure proper cleanup!

Apart from that there is another way:

require 'set'  
addresses = Set.new

File.foreach "data.txt" do |line|  
&nbsp;&nbsp;line.chomp!  
&nbsp;&nbsp;line.downcase!

&nbsp;&nbsp;puts "Duplicate: #{line}" unless addresses.add? line  
end

Cheers

robert

> **···**
>
> 2008/2/5, 7stud -- \<bbxx789\_05ss@yahoo.com\>:
> 
> > Lovell Mcilwain wrote:  
> > \>  
> > \> The code I posted is exactly what I ran aside for giving you  
> > \> hundrededs of lines of email. The example is exactly what I ran to  
> > \> get the results I posted.  
> > \>
> > 
> > emails = File.open("data.txt").readlines.map! {|x| x.chomp}  
> > email.scan(/\S+/)
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

### Author: ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)
#### Post date: [5 February 2008 12:24 UTC](https://rubytalk.org/t/string-scanning-woes/44265/10 "2008-02-05T12:24:55Z")

</div>

h = {}  
File.foreach("data"){|e|  
&nbsp;&nbsp;e = e.strip.upcase  
&nbsp;&nbsp;puts "Duplicate: #{ e }" if h.include? e  
&nbsp;&nbsp;h[e] = true  
}

> **···**
>
> On Feb 5, 3:52 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > require 'set'  
> > addresses = Set.new
> > 
> > File.foreach "data.txt" do |line|  
> > &nbsp;&nbsp;line.chomp!  
> > &nbsp;&nbsp;line.downcase!
> > 
> > &nbsp;&nbsp;puts "Duplicate: #{line}" unless addresses.add? line  
> > end

---

<div class="post-metadata">

### Author: ![Lovell](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@Lovell](https://rubytalk.org/u/Lovell)
#### Post date: [5 February 2008 16:39 UTC](https://rubytalk.org/t/string-scanning-woes/44265/11 "2008-02-05T16:39:57Z")

</div>

Thanks guys for all the helpful hints.

> **···**
>
> On Feb 5, 7:20 am, William James \<w\_a\_x\_...@yahoo.com\> wrote:
> 
> > On Feb 5, 3:52 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> > 
> > \> require 'set'  
> > \> addresses = Set.new
> > 
> > \> File.foreach "data.txt" do |line|  
> > \> line.chomp!  
> > \> line.downcase!
> > 
> > \> puts "Duplicate: #{line}" unless addresses.add? line  
> > \> end
> > 
> > h = {}  
> > File.foreach("data"){|e|  
> > e = e.strip.upcase  
> > puts "Duplicate: #{ e }" if h.include? e  
> > h[e] = true
> > 
> > }
