Arc Forumnew | comments | leaders | submitlogin
String matching
2 points by goolu 6083 days ago | 7 comments
Hi, I am new to lisp and was wondering how can i do string operation. eg- (= title '(yahoo yahoogoogle "google yahoo"))

i want write function that will search for string google and return it. I am looking for minimalistic approach and thanks in advance.



2 points by thaddeus 6083 days ago | link

I don't have my environment up as I am at work, but it should work something like:

(find "google" '(yahoo yahoogoogle "google yahoo"))

but it looks like you need to find "google" inside a string thats inside a list:

To find a sub-string use:

    Arc>(findsubseq "google" "google yahoo")
    0
so essentially if the string "google" exists inside the other string you will get the start position returned otherwise you will get nil.

Then you could:

    (def test-string-for-sub-string (mystring thestring)
      (= result (findsubseq mystring thestring))
      (if (isnt result nil)
          (prn mystring))
    )

    Arc> (test-string-for-sub-string "google" "google yahoo")
    "google"
and then do something like:

    (def test-list-for-substring (mystring)
      (= item-list '(yahoo yahoogoogle "google yahoo"))
      (each item item-list 
            (if (isa item 'string)
                 (test-string-for-sub-string mystring item))
      )
    )
I am not suggesting this is the best way, I'm just laying it out so that you understand how the list and string functions work to achieve what you would like to do.

Like I said I don't have arc to verify what I wrote evaluates correctly, but I think its close.

Also note: I found arcfn.com to be a great resource.... you can go to:

    http://arcfn.com/doc/index.html

    then click the strings operations link:

    http://arcfn.com/doc/string.html
T.

-----

2 points by thaddeus 6083 days ago | link

and taking a second look: I also see a symbol in the list that has google in it.

If you need the symbol 'yahoogoogle to find google too, then you may want to convert each item in the list to a string then do your check?:

    (def test-list-for-substring (mystring)
      (= item-list '(yahoo yahoogoogle "google yahoo"))
      (each item item-list 
            (= string-item (tostring item))
            (test-string-for-sub-string mystring string-item))
      )
    )

-----

1 point by goolu 6083 days ago | link

I really appreciate your help. I will try this later tonight. thanks

-----

1 point by thaddeus 6083 days ago | link

no problem.

P.S.You may want to check out the function 'in'. Depends on how you're pulling the items to check against. If it's a list as you suggested then 'find', or looping through 'each' is probably the best, but if they are separate arguments 'in' may be easier. T.

-----

1 point by goolu 6083 days ago | link

awesome. second approach (tostring) worked. I tried to put your code and came up with this: (def test-list (mystring item-list) (each item item-list (if (findsubseq mystring (tostring (pr item))) (prn item)) ) )

arc> (test-list "google" '(yahoo yahoogoogle "google yahoo")) yahoogoogle google yahoo

i was trying to use car on list..but it dint worked. Thanks

-----

1 point by thaddeus 6083 days ago | link

As a note - you dont need (tostring (pr item)) ....unless you want the output of each result to be a string. you could just use (string item).

Here's another method just for the sake of learning:

This method does a few things..(which maybe good or bad).

     * it returns it in a list format for re-use.
     * it maintains the original 'type'.
     * it still preserves the original order.

    (def test-list (mystring item-list) 
         (rev (accum tolist 
	          (each item item-list 
		      (if (findsubseq mystring (string item))
			      (tolist item)))))) 


    arc> (test-list "google" '(yahoo yahoogoogle "google yahoo"))
    (yahoogoogle "google yahoo")

-----

1 point by goolu 6083 days ago | link

awesome. second approach (tostring) worked. I tried to put your code and came up with this: (def test-list (mystring item-list) (each item item-list (if (findsubseq mystring (tostring (pr item))) (prn item)) ) )

arc> (test-list "google" '(yahoo yahoogoogle "google yahoo")) yahoogoogle google yahoo

i was trying to use car on list..but it dint worked. Thanks

-----