From: Matthew Kerwin Date: 2013-02-04T11:38:26+09:00 Subject: Re: Class for data analysis: File.open once --f46d042ef7492ba70b04d4dcf5e0 Content-Type: text/plain; charset=ISO-8859-1 On 4 February 2013 12:03, Soichi Ishida wrote: > Thanks everyone. They will certainly help. > > In addition to these, what if I want to apply specific regular > expressions to each of the lines? > > say, > > if line =~ /exp1/ then > # do stuff > else > # do stuff > end > > In this case, the loop for all lines must be implemented, am I correct? > The previous code already does that, and we showed that it can be done in one loop. > If the class methods dynamically take regular expression like > > test = DataExtractor.new('file.txt') > test.apply_regexp(/exp1/) > > Doesn't the loop need to be reactivated for each method like this? Yes, if the regex isn't necessarily known beforehand you'll have to do the loop again once it is known. Note that, as Marcus pointed out, you're reading the file into memory; if you are happy trading IO delays for permanent memory usage, you could store the file contents in an instance variable (either in the constructor or lazily) and then perform your variable pattern match on the stored file contents. It would still need to perform the loop many times, but would save a lot of time in the #open and #read phases. There are other caching techniques, as well, if you think a particular pattern is likely to be tested multiple times. Probably a bit advanced for this discussion, though. -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/ ABN: 59-013-727-651 "You'll never find a programming language that frees you from the burden of clarifying your ideas." - xkcd --f46d042ef7492ba70b04d4dcf5e0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On 4 February 2013 12:03, Soichi Ishida = <lists@ruby-fo= rum.com> wrote:
Thanks everyone. They will certainly help.
In addition to these, what if I want to apply specific regular
expressions to each of the lines?

say,

if line =3D~ /exp1/ then
=A0# do stuff
else
=A0# do stuff
end

In this case, the loop for all lines must be implemented, am I correct?
=

The previous code already does that,= and we showed that it can be done in one loop.
=A0
If the class methods dynamically take regular expression like

test =3D DataExtractor.new('file.txt')
test.apply_regexp(/exp1/)

Doesn't the loop need to be reactivated for each method like this?
=A0
Yes, if the regex isn't necessarily kn= own beforehand you'll have to do the loop again once it is known. =A0No= te that, as Marcus pointed out, you're reading the file into memory; if= you are happy trading IO delays for permanent memory usage, you could stor= e the file contents in an instance variable (either in the constructor or l= azily) and then perform your variable pattern match on the stored file cont= ents. =A0It would still need to perform the loop many times, but would save= a lot of time in the #open and #read phases.

There are other caching techniques, as well= , if you think a particular pattern is likely to be tested multiple times. = =A0Probably a bit advanced for this discussion, though.
--
=A0 Matthew Kerwin, B.Sc (CompSci) (Hons)
=A0 http://matthew.kerwin.net.au= /
=A0 ABN: 59-013-727-651

=A0 "You'll never find a p= rogramming language that frees
=A0 you from the burden of clarifying your ideas." - xkcd
--f46d042ef7492ba70b04d4dcf5e0--