Regex or line.each seems to not be working.

B

Brad Mr

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
 
B

Brad Mr

Roger said:
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")
 
M

Marvin Gülker

Brad said:
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
 
B

Brad Mr

Marvin said:
Brad said:
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.
 
R

Roger Pack

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
 
R

Robert Klemme

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. :)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top