[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

removing newline from eachline in file

Charles Pareto

9/18/2007 8:14:00 PM

I'm reading a file line by line and trying to remove the newline at the
end of each line so that I can rebuild the url with the "www" and the
".net" at the beginning and end. Does anyone know how I can remove the
newline char at the end of each line? Also if the url doesn't work/exist
I don't want the script to crash I want to keep moving through the file
its reading.

begin
while (line = f.readline)
line = line =~ /(.*?)\n/
url = "www." + line + ".net"

open(url) { |page| page_content = page.read()
puts "link exists"
}
end
rescue
puts "link does not exist"

rescue EOFError
f.close
end
--
Posted via http://www.ruby-....

6 Answers

7stud 7stud

9/18/2007 8:54:00 PM

0

Chuck Dawit wrote:
> I'm reading a file line by line and trying to remove the newline at the
> end of each line so that I can rebuild the url with the "www" and the
> ".net" at the beginning and end. Does anyone know how I can remove the
> newline char at the end of each line?

See String#chomp:

require "open-uri"

File.open("aaa.txt", "w") do |file|
file.print("hello\n", "world\n", "goodbye\n")
end

File.open("aaa.txt") do |file|
file.each do |line|
print line.chomp
end
puts
end

--output:--
helloworldgoodbye
--
Posted via http://www.ruby-....

Tim Hunter

9/18/2007 9:01:00 PM

0

Chuck Dawit wrote:
> I'm reading a file line by line and trying to remove the newline at the
> end of each line so that I can rebuild the url with the "www" and the
> ".net" at the beginning and end. Does anyone know how I can remove the
> newline char at the end of each line? Also if the url doesn't work/exist
> I don't want the script to crash I want to keep moving through the file
> its reading.


ri String#chomp
----------------------------------------------------------- String#chomp
str.chomp(separator=$/) => new_str
------------------------------------------------------------------------
Returns a new String with the given record separator removed from
the end of str (if present). If $/ has not been changed from the
default Ruby record separator, then chomp also removes carriage
return characters (that is it will remove \n, \r, and \r\n).

"hello".chomp #=> "hello"
"hello\n".chomp #=> "hello"
"hello\r\n".chomp #=> "hello"
"hello\n\r".chomp #=> "hello\n"
"hello\r".chomp #=> "hello"
"hello \n there".chomp #=> "hello \n there"
"hello".chomp("llo") #=> "he"

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

7stud 7stud

9/18/2007 9:04:00 PM

0

Chuck Dawit wrote:
> Also if the url doesn't work/exist
> I don't want the script to crash I want to keep moving through the file
> its reading.

My isp redirects faulty urls to an error page, so all urls return a web
page. I think you're going to have to use Net::HTTP to look at the
headers of the response, and then take some action based on the header
value.
--
Posted via http://www.ruby-....

7stud 7stud

9/18/2007 9:17:00 PM

0

Chuck Dawit wrote:
> Also if the url doesn't work/exist
> I don't want the script to crash I want to keep moving through the file
> its reading.
>
> begin
> while (line = f.readline)
> line = line =~ /(.*?)\n/
> url = "www." + line + ".net"
>
> open(url) { |page| page_content = page.read()
> puts "link exists"
> }
> end
> rescue
> puts "link does not exist"
>
> rescue EOFError
> f.close
> end

If your program is currently crashing on bad urls, you just need to put
the begin/rescue around the open statement---not outside your loop.
Something like this(untested):

begin
while (line = f.readline)
line = line =~ /(.*?)\n/
url = "www." + line + ".net"

begin
open(url) { |page| page_content = page.read() }
puts "link exists"
rescue Exception
puts "in rescue"
#do nothing -- when you catch an exception the exception
goes away

puts "execution continues on its way"
end
end

--
Posted via http://www.ruby-....

Tom Reilly

9/18/2007 9:23:00 PM

0

Try changing the code to:
file_in = File.new("whatever")
file_in.each do |x|
url = x.chomp + ".net"

gud luck


Chuck Dawit wrote:
> I'm reading a file line by line and trying to remove the newline at the
> end of each line so that I can rebuild the url with the "www" and the
> ".net" at the beginning and end. Does anyone know how I can remove the
> newline char at the end of each line? Also if the url doesn't work/exist
> I don't want the script to crash I want to keep moving through the file
> its reading.
>
> begin
> while (line = f.readline)
> line = line =~ /(.*?)\n/
> url = "www." + line + ".net"
>
> open(url) { |page| page_content = page.read()
> puts "link exists"
> }
> end
> rescue
> puts "link does not exist"
>
> rescue EOFError
> f.close
> end
>


Peña, Botp

9/19/2007 3:54:00 AM

0

# line = line =~ /(.*?)\n/

what does this line of code do?