Regex or line.each seems to not be working

I have been banging my head over this for 2 days. The following is to
scan a regular text file line by line using regular expressions. it
seem to only grab the first line and not iterate through all the lines
like it should. The “@st == True” is used for Try and Rescue for
possible failed telnet sessions, and move on to the next.

CODE:

CE200s and COMMANDS

def dslam_list()
@rf.each do |line|
if line.scan(/^N\w\w\w/)
@dns = line.to_s.chomp()
telnet_to_dslam(@dns, @pf)
if @st == true
login_to_dslam(@un, @pw)
end
end

  if line.scan(/^get.+/)
    @command = line.to_s.chomp()
    give_commands(@command)
  end

  if line.scan(/^$/)
    sleep (1)
    exit_commands()
  end
end

# close files after finsished with io
@rf.close()
@of.close()

end

TEXT FILE:
The first line is the Host name that is used by telnet method. Telnet
works fine.

N2CA
get cmsystem
get udp
get system

N7CA
get cmsystem
get udp
get system

@rf.each do |line|

does rf have multiple lines in it?
(you can add a puts line in there, to see).
-r

Roger P. wrote:

@rf.each do |line|

does rf have multiple lines in it?
(you can add a puts line in there, to see).
-r

Yes… @rf is actually the instance variable of the text file listed at
the end of my original posting

Instantiated as such:

@rf = File.open("#{Dir.pwd}/Script_Files/BlahBlahBlah.txt, “r”)

Brad Mr wrote:

if line.scan(/^N\w\w\w/)
String#scan never returns false. I suppose you want #match:

ruby -v: ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
irb(main):001:0> “foo”.scan(/^xxx/)
=> []
irb(main):002:0> “foo”.match(/^xxx/)
=> nil
irb(main):003:0>

Marvin

Perfect… Thanx man that did the trick… Changed everything to
“match” instead of “scan”… Keep a mental not on that.

Believe it or not, I run into that one all the time

Perhaps there should be an RCR for scan to return nil on no match?

-r

On 01/28/2010 08:45 PM, Roger P. wrote:

Perfect… Thanx man that did the trick… Changed everything to
“match” instead of “scan”… Keep a mental not on that.

Believe it or not, I run into that one all the time

Perhaps there should be an RCR for scan to return nil on no match?

IMHO that would be inconsistent. Think about how SQL does it: if you
query for one element, you get a result set with one element which can
be NULL or a value != NULL. If you query for multiple elements you get
a potentially empty result set. I believe the number of APIs that
return collections and which return nil, null or NULL in case nothing is
found is a minority.

Btw, I usually use #scan with the block. In that case I don’t really
care about the return value of #scan. :slight_smile:

Kind regards

robert

Marvin Gülker wrote:

Brad Mr wrote:

if line.scan(/^N\w\w\w/)
String#scan never returns false. I suppose you want #match:

ruby -v: ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
irb(main):001:0> “foo”.scan(/^xxx/)
=> []
irb(main):002:0> “foo”.match(/^xxx/)
=> nil
irb(main):003:0>

Marvin

Perfect… Thanx man that did the trick… Changed everything to
“match” instead of “scan”… Keep a mental not on that.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs