Login  Register

OrderedCollection get all lines

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

OrderedCollection get all lines

Valentin Ryckewaert
54 posts
Hi,

I would like to reproduce the grep command in Pharo.
To do it I wanted to get the content of a stream and transform it to an OrderedCollection (to use matchesRegex: on it).

First of all, is there an easier way to do it?
In a second time, is there a method to get the content of a Stream as an OrderedCollection ? (nextLine exists so I could use it to get my OrderedCollection but is there an easier way?)
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: OrderedCollection get all lines

Sven Van Caekenberghe-2
5697 posts
Did you see #lines and #linesDo: ?

> On 11 May 2016, at 16:15, Valentin Ryckewaert <[hidden email]> wrote:
>
> Hi,
>
> I would like to reproduce the grep command in Pharo.
> To do it I wanted to get the content of a stream and transform it to an OrderedCollection (to use matchesRegex: on it).
>
> First of all, is there an easier way to do it?
> In a second time, is there a method to get the content of a Stream as an OrderedCollection ? (nextLine exists so I could use it to get my OrderedCollection but is there an easier way?)


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: OrderedCollection get all lines

Valentin Ryckewaert
54 posts
Thanks you again Sven, it will work with that, but there is nothing on Streams? 


2016-05-11 16:19 GMT+02:00 Sven Van Caekenberghe <[hidden email]>:
Did you see #lines and #linesDo: ?

> On 11 May 2016, at 16:15, Valentin Ryckewaert <[hidden email]> wrote:
>
> Hi,
>
> I would like to reproduce the grep command in Pharo.
> To do it I wanted to get the content of a stream and transform it to an OrderedCollection (to use matchesRegex: on it).
>
> First of all, is there an easier way to do it?
> In a second time, is there a method to get the content of a Stream as an OrderedCollection ? (nextLine exists so I could use it to get my OrderedCollection but is there an easier way?)



Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: OrderedCollection get all lines

Sven Van Caekenberghe-2
5697 posts

> On 11 May 2016, at 16:22, Valentin Ryckewaert <[hidden email]> wrote:
>
> Thanks you again Sven, it will work with that, but there is nothing on Streams?

If performance is not too important, just read the whole contents of the file.

BTW, this is actually example 19 of 'Elegant Pharo Code':

https://medium.com/concerning-pharo/elegant-pharo-code-bb590f0856d0

'^.*.jpg' asRegex in: [ :regex |
  '/tmp/foo.txt' asFileReference contents lines
    select: [ :line | regex matches: line ] ]

> 2016-05-11 16:19 GMT+02:00 Sven Van Caekenberghe <[hidden email]>:
> Did you see #lines and #linesDo: ?
>
> > On 11 May 2016, at 16:15, Valentin Ryckewaert <[hidden email]> wrote:
> >
> > Hi,
> >
> > I would like to reproduce the grep command in Pharo.
> > To do it I wanted to get the content of a stream and transform it to an OrderedCollection (to use matchesRegex: on it).
> >
> > First of all, is there an easier way to do it?
> > In a second time, is there a method to get the content of a Stream as an OrderedCollection ? (nextLine exists so I could use it to get my OrderedCollection but is there an easier way?)
>
>
>


Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: OrderedCollection get all lines

Henrik Sperre Johansen
1774 posts
There's also PositionableStream >> #nextLine
which lets you write code like

[readStream atEnd] whileFalse:
   [(self regexMatch: readStream nextLine)
       ifNotNil: [:matchingLine | "Record match somewhere here"]]

Cheers,
Henry