[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

pulling out a specfic feild from a line

Peter Loftus

12/13/2007 3:33:00 PM

Hey guys

Ive been looking for awhile now today trying to find a decent example of
this and im really fustrated with it now.
I need to get a specfic section of a line.
lets sat this is the line

example 1 example 2 example 3

I want to get the third section "example 3"so i need to use tab as the
sperater any suggestions?

Even a pointer to a section in the API that might help me would be
usefull

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

7 Answers

Peter Loftus

12/13/2007 3:34:00 PM

0

Sorry about the spelling btw
--
Posted via http://www.ruby-....

Sebastian Hungerecker

12/13/2007 3:47:00 PM

0

Peter Loftus wrote:
> lets sat this is the line
>
> example 1 example 2 example 3
>
> I want to get the third section "example 3"so i need to use tab as the
> sperater any suggestions?

"example 1\texample 2\texample 3".split("\t")[2]

HTH,
Sebastian
=2D-=20
NP: Die =C3=84rzte - Red Mit Mir
Jabber: sepp2k@jabber.org
ICQ: 205544826

Peter Loftus

12/13/2007 4:29:00 PM

0

ok but lets say im looping through an array full of different lengths
and layouts of strings for instance

example example example
exmaple/dfsd/asdf/ asdfasdf
example 1 example2 example 3
laskdjflkasdjfkl

im looking to see if the that third string on line 3 is actually there
so passing very line into array and then checking each line

arr = Array.new
i = 0
File.foreach(example.txt) do |line|
arr[i] = line
i += 1
end
arr.each do |item|
if (arr.to_s.split("\t")[2] = "example 3")
puts "found it"
end

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

Sebastian Hungerecker

12/13/2007 4:44:00 PM

0

Peter Loftus wrote:
> arr =3D Array.new
> i =3D 0
> File.foreach(example.txt) do |line|
> =C2=A0 =C2=A0arr[i] =3D line
> =C2=A0 =C2=A0i +=3D 1
> end

arr =3D File.readlines("example.txt")
Does the same as the code above, but is far more compact.

> arr.each do |item|
> if (arr.to_s.split("\t")[2] =3D "example 3")
> =C2=A0 puts "found it"
> end
arr.to_s will give you the whole content of the file. You want to use item.

arr.map! {|line| line.split("\t")[2]}
arr.each_with_index do |field, i|
if field
puts "Found field #{field} in line #{i}"
else
puts "No third field in line #{i}"
end
end


HTH,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

Robert Klemme

12/14/2007 8:45:00 AM

0

2007/12/13, Peter Loftus <loftuz@gmail.com>:
> ok but lets say im looping through an array full of different lengths
> and layouts of strings for instance
>
> example example example
> exmaple/dfsd/asdf/ asdfasdf
> example 1 example2 example 3
> laskdjflkasdjfkl
>
> im looking to see if the that third string on line 3 is actually there
> so passing very line into array and then checking each line
>
> arr = Array.new
> i = 0
> File.foreach(example.txt) do |line|
> arr[i] = line
> i += 1
> end
> arr.each do |item|
> if (arr.to_s.split("\t")[2] = "example 3")

You have an assignment here which is not what you want here.

> puts "found it"
> end

This is a very inefficient way of doing things. You don't need to
store the whole file in an array if you are just interested in the
presence test. Btw, you can even do this:

ruby -anF\\t -e 'if $F[2] == "example 3"; puts "found"; exit 0; end;
END{ raise "not found"}' your_file

But then you'd probably use (g)awk anyway. :-)

So, going back to Ruby:

File.foreach "file.txt" do |line|
if line.chomp.split(/\t/)[2] == "example 3"
puts "found"
break # or: return or exit
end
end

Probably a bit more efficient:

File.foreach "file.txt" do |line|
if /^(?:[^\t]*\t){2}example 3\t/ =~ line
puts "found"
break # or: return or exit
end
end

Kind regards

robert


--
use.inject do |as, often| as.you_can - without end

Peter Loftus

12/14/2007 9:17:00 AM

0

> arr.map! {|line| line.split("\t")[2]}
> arr.each_with_index do |field, i|
> if field
> puts "Found field #{field} in line #{i}"
> else
> puts "No third field in line #{i}"
> end
> end
>

thanks alot!

if i want to check that fields value how do i check it like it prints it
out to the screen but i want to use something like this

if field = "example 3"
puts "Found field #{field} in line #{i}"
end
but its still just spitting out each line

I dont want to print each line that has a third field just the specic
one



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

Peter Loftus

12/14/2007 10:06:00 AM

0

I got it

flag = "FALSE"
arr = File.readlines(var1)
arr.map! {|line| line.split("\t")[2]}
arr.each_with_index do |line, i|
if /#{var2}/ =~ line
flag = "TRUE"
end
end
if (flag == "TRUE")
puts "SUCCESS: This file " +var1 + " does contain the line"
end
if (flag == "FALSE")
puts "ERROR: This file " +var1 + " does not contain the line"
end

thanks for the help
--
Posted via http://www.ruby-....