Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grep function?
#1
hello,

i can't find a grep function to extract line text conatining a pattern and the line number

file to test :

myfile.txt

I like one
i like two
i like three
i like four

i acheive that doing

str match Fichier.getfile(myfile.txt)
ARRAY(str) toto
toto=Fichier
for int'i 0 toto.len
if(findrx(toto[i] "like" 0 1 match)<0) continue
else
out "the line containing ''like'' is %s" toto[i]
out "the number of the line is %i" i

Anyway simplier?
#2
findrx can find a line but will not tell line number. Need split into lines. Faster will be with foreach

Macro Macro1103
Code:
Copy      Help
str match Fichier.getfile(myfile.txt)
int i=0
foreach _s Fichier
,i+1
,if(findrx(_s "like" 0 1 match)<0) continue
,out "the line containing ''like'' is %s" _s
,out "the number of the line is %i" i-1
#3
int i=0
foreach _s Fichier
,i+1
,if(findrx(_s "like" 0 1 match)<0) continue
,out "the line containing ''like'' is %s" _s
,out "the number of the line is %i" i-1

question 1: is _s variable a variable created directly by the macro when called? what type is it?

question 2: in fact my function is a bit more complex and would be:
str match Fichier.getfile(myfile.txt)
ARRAY(str) u=Fichier
ARRAY(str) o.create(0(line text) 0(line number))
Grep(&u &match &o)
and i'd like to store the pair (_s i) in a two dim array o as result, but it does not work Sad
so i can out for instance : out "the line %i containing %match is %s" o[line number] match o[line text]

How?
#4
1. yes. Cick _s and press F1.

2.
Macro Macro1103
Code:
Copy      Help
str _file="$desktop$\led.txt"
str rx="li.?e"
out

type MYGREPRESULT str'lineText lineIndex
ARRAY(MYGREPRESULT) a

str match Fichier.getfile(_file)
int i=0
foreach _s Fichier
,i+1
,if(findrx(_s rx 0 1 match)<0) continue
,MYGREPRESULT& r=a[] ;;add element
,r.lineIndex=i-1
,r.lineText=_s

;out results
for i 0 a.len
,MYGREPRESULT& rr=a[i]
,out "the line %i containing %s is %s" rr.lineIndex rx rr.lineText
#5
1. hmm, should have done it by myself :/

2. was miles away from thinking i add to declare my own type of variable to do that. If i missed it from help file, my bad, if not maybe a add-on in help file can be useful.

Last one : i want call grep function that way

grep(&arraycontainingtexttoparse &texttomatch &myresultmutidimarray)

as myresultmutidimarray can be any type i tried variant to catch any array type:

function ARRAY(str)&_Array str&_Pattern ARRAY(VARIANT)&_Result


but i get a type mismatch error on &myresultmutidimarray . What is my error?
from help: VARIANT any, except structures ?
As always, great help.
#6
Hi Gintaras,

no opinion on that?

Thanks.
#7
If in function declaration _Result is of type ARRAY(VARIANT), you must pass a variable of type ARRAY(VARIANT) to the function.

When passing by value, values of some types are converted to other types. When by reference - never.
#8
Ok then, so is there a way to achieve that?
#9
Variables of what types would be passed?
#10
type MYGREPRESULT str'lineText lineIndex
ARRAY(myresultmutidimarray) myresultmutidimarray

grep(&arraycontainingtexttoparse(type=ARRAY(str) &texttomatch(type=str) &myresultmutidimarray(type=MYGREPRESULT )
#11
function ... !*_Result resultType ;;resultType: 0 type1, 1 type2, ...

sel resultType
,case 1
,...
,case2
,...
,...
#12
but probably better to create several functions, for each type. If they contain common code, place it in a separate function.
#13
Hmm, sorry but i don't have a clue of what that means.

Will try some code when back at home.

Will post result later.

Thanks
#14
When an argument is of type !*, which is shorter form of byte*, you can pass address of variables of any type. Like void* in C++. Use 4-th argument to specify type.


Forum Jump:


Users browsing this thread: 1 Guest(s)