[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Search File Contents

Andy Dbest

6/8/2008 2:06:00 PM

Hi Guys,

I have a small code block that is frustrating me for some time now. I
want to search for some text in a file. Simple isnt it?

def read_file week_num
week_num = week_num.to_s
f = File.open("fixtures.txt", "r")
lines = f.readlines
#week_num = '1.'
lines.each do|line|
if line.chomp.eql? week_num
puts "Found the week number"
end
end
f.close
end

The above is the method, and I am searching for "1.". I am passing the
variable "week_num" and i even tried to convert it to string. The search
code as it is right now, does not output "Found the week number", even
though the file contains "1."

If i uncomment the line that assigns week_num to '1.' directly, then it
works. What am i doing wrong?
--
Posted via http://www.ruby-....

10 Answers

Stefano Crocco

6/8/2008 2:23:00 PM

0

On Sunday 08 June 2008, Andy Dbest wrote:
> Hi Guys,
>
> I have a small code block that is frustrating me for some time now. I
> want to search for some text in a file. Simple isnt it?
>
> def read_file week_num
> week_num = week_num.to_s
> f = File.open("fixtures.txt", "r")
> lines = f.readlines
> #week_num = '1.'
> lines.each do|line|
> if line.chomp.eql? week_num
> puts "Found the week number"
> end
> end
> f.close
> end
>
> The above is the method, and I am searching for "1.". I am passing the
> variable "week_num" and i even tried to convert it to string. The search
> code as it is right now, does not output "Found the week number", even
> though the file contains "1."
>
> If i uncomment the line that assigns week_num to '1.' directly, then it
> works. What am i doing wrong?

If assiging week_num a value inside the method makes the code work, I'd say
the problem is in the argument which is passed to the method. Have you tried
to put a

puts week_num

at the beginning of the method, to see whether the argument you get is the one
you expect?

Stefano


Andy Dbest

6/8/2008 2:28:00 PM

0

Stefano Crocco wrote:
> On Sunday 08 June 2008, Andy Dbest wrote:
> If assiging week_num a value inside the method makes the code work, I'd
> say
> the problem is in the argument which is passed to the method. Have you
> tried
> to put a
>
> puts week_num
>
> at the beginning of the method, to see whether the argument you get is
> the one
> you expect?
>
> Stefano

I tried the puts line, just inside the method and it does return '1.'
for me. It kind of annoying...
--
Posted via http://www.ruby-....

Stefano Crocco

6/8/2008 2:48:00 PM

0

On Sunday 08 June 2008, Andy Dbest wrote:
> Stefano Crocco wrote:
> > On Sunday 08 June 2008, Andy Dbest wrote:
> > If assiging week_num a value inside the method makes the code work, I'd
> > say
> > the problem is in the argument which is passed to the method. Have you
> > tried
> > to put a
> >
> > puts week_num
> >
> > at the beginning of the method, to see whether the argument you get is
> > the one
> > you expect?
> >
> > Stefano
>
> I tried the puts line, just inside the method and it does return '1.'
> for me. It kind of annoying...

Can you post a sample of the file you're searching in? This way, I can try
your code myself and see what's going on.

Stefano

Andy Dbest

6/8/2008 3:01:00 PM

0

Stefano Crocco wrote:
> On Sunday 08 June 2008, Andy Dbest wrote:
>> > at the beginning of the method, to see whether the argument you get is
>> > the one
>> > you expect?
>> >
>> > Stefano
>>
>> I tried the puts line, just inside the method and it does return '1.'
>> for me. It kind of annoying...
>
> Can you post a sample of the file you're searching in? This way, I can
> try
> your code myself and see what's going on.
>
> Stefano

sure...

here is an extract of the file

1.

aaa.txt - bbb.txt
aac.txt - bbc.tct

2.

aac.txt - bbb.txt
aaa.txt - bbc.txt
--
Posted via http://www.ruby-....

Stefano Crocco

6/8/2008 3:11:00 PM

0

On Sunday 08 June 2008, Andy Dbest wrote:
> Stefano Crocco wrote:
> > On Sunday 08 June 2008, Andy Dbest wrote:
> >> > at the beginning of the method, to see whether the argument you get is
> >> > the one
> >> > you expect?
> >> >
> >> > Stefano
> >>
> >> I tried the puts line, just inside the method and it does return '1.'
> >> for me. It kind of annoying...
> >
> > Can you post a sample of the file you're searching in? This way, I can
> > try
> > your code myself and see what's going on.
> >
> > Stefano
>
> sure...
>
> here is an extract of the file
>
> 1.
>
> aaa.txt - bbb.txt
> aac.txt - bbc.tct
>
> 2.
>
> aac.txt - bbb.txt
> aaa.txt - bbc.txt

I've tried it and it works even without the assignment. Are you sure there
isn't any trailing space afer 1. in the method argument? Using puts as I
suggested in my first post wouldn't have shown that. You can try examining the
argument with p, instead of puts, which displays the string with special
characters escaped and surrounded by quotes. If everything is correct, the
line

p week_num

should output

"1."

If there are trailing spaces, you'll see something like

"1. "

Stefano


Andy Dbest

6/8/2008 3:25:00 PM

0

Stefano Crocco wrote:
> On Sunday 08 June 2008, Andy Dbest wrote:

> I've tried it and it works even without the assignment. Are you sure
> there
> isn't any trailing space afer 1. in the method argument? Using puts as I
> suggested in my first post wouldn't have shown that. You can try
> examining the
> argument with p, instead of puts, which displays the string with special
> characters escaped and surrounded by quotes. If everything is correct,
> the
> line
>
> p week_num
>
> should output
>
> "1."
>
> If there are trailing spaces, you'll see something like
>
> "1. "
>
> Stefano

Thanks Stefano,

the p week_num line did the trick, i noticed that there was a leading
space. I was taking the input from an FXRuby text control, in which i
introduced a space.

Have fixed it now.

Cheers and thanks.
Andy
--
Posted via http://www.ruby-....

baldeagle

9/18/2008 9:32:00 PM

0

On Sep 19, 1:19 am, Dirty Sick Pig <drtysicpig@pigs_lodge_1.org>
wrote:
> baldeagle wrote:
> > On Sep 18, 6:08 am, Dirty Sick Pig <drtysicpig@pigs_lodge_1.org>
> > wrote:
> >> baldeagle wrote:
> >>> On Sep 17, 12:29 pm, Dirty Sick Pig <drtysicpig@pigs_lodge_1.org>
> >>> wrote:
>
> >> Yes.  Our society is so open that even a lowly clerk in the Treasury
> >> Department, printer's devil or engraver in the presses and mints can
> >> determine and know what the fuck if going on,
>
> >> ...Thousands of individual, private  Americans operate those printing
> >> presses.
>
> > Really ???
>
> > Would the printing clerks, engineers know how much money
> > ALL the printing presses produced in total each day, each
> > month ?
>
> Not each month.  EACH DAY!  And every American and legal resident can
> demand the information under our FREEDOM OF INFORMATION law.  

Have you demand ...the info on how much money
the US government printed in excess of those
destroyed?

Tell us.

Dirty Sick Pig

9/18/2008 10:14:00 PM

0

baldeagle wrote:
> On Sep 19, 1:19 am, Dirty Sick Pig <drtysicpig@pigs_lodge_1.org>
> wrote:
>> baldeagle wrote:
>>> On Sep 18, 6:08 am, Dirty Sick Pig <drtysicpig@pigs_lodge_1.org>
>>> wrote:
>>>> baldeagle wrote:
>>>>> On Sep 17, 12:29 pm, Dirty Sick Pig <drtysicpig@pigs_lodge_1.org>
>>>>> wrote:
>>>> Yes. Our society is so open that even a lowly clerk in the Treasury
>>>> Department, printer's devil or engraver in the presses and mints can
>>>> determine and know what the fuck if going on,
>>>> ...Thousands of individual, private Americans operate those printing
>>>> presses.
>>> Really ???
>>> Would the printing clerks, engineers know how much money
>>> ALL the printing presses produced in total each day, each
>>> month ?
>> Not each month. EACH DAY! And every American and legal resident can
>> demand the information under our FREEDOM OF INFORMATION law.
>
> Have you demand ...the info on how much money
> the US government printed in excess of those
> destroyed?

I didn't have to because there's no reason for me to do so. I read the
monthly and quarterly reports to the President and to Congress via its
own auditing service, the GAO. The reports are also turned over to our
newspapers. Those news articles as written, baldostritch, are easy to
understand, even for reading and comprehension challenged people like
you, who start conspiracy theories whenever they don't understand simple
things.

> Tell us.

I just did and I just told you off, idiot.

baldeagle

9/18/2008 10:27:00 PM

0


> > Have you demand ...the info on how much money
> > the US government printed in excess of those
> > destroyed?
>
> I didn't have to because there's no reason for me to do so.  
>

The info is NOT available.

Your claim about freedom of Info is a sham...
only fools will believe in this.
Quote: "> > ....every American and legal resident can
> > demand the information under our FREEDOM OF INFORMATION law. "

Dirty Sick Pig

9/18/2008 10:50:00 PM

0

baldeagle wrote:
>>> Have you demand ...the info on how much money
>>> the US government printed in excess of those
>>> destroyed?
>> I didn't have to because there's no reason for me to do so.
>>
>
> The info is NOT available.

It is, even to residents of mental wards like you as long as you are
entitled to the information (U.S. citizen or resident). If you were
told something is not available, that means it never existed.

> Your claim about freedom of Info is a sham...
> only fools will believe in this.
> Quote: "> > ....every American and legal resident can
>>> demand the information under our FREEDOM OF INFORMATION law. "

No shit? Bwahahahahahahahahahahawr! Stupid, idiot, moron, imbecile,
baldostritch.

If you need my help with FOIA, ask nicely, ok, birdbrain?