How to check if cell values exist in an array?

306 views
Skip to first unread message

Yogesh

unread,
Aug 3, 2017, 10:04:04 AM8/3/17
to OpenRefine
I have a well-defined list of states (master list). How do I find wether the value in the STATENAME matches the value in the master list?

For e.g. 

cells[STATENAME].value IN masterlistofstates
 
Thanks in advance.

Ettore Rizza

unread,
Aug 3, 2017, 11:45:11 PM8/3/17
to OpenRefine
Hi Yogesh, 

you will find a very similar case in the documentation.

Using Python / Jython rather than GREL, you can add a column based on your column STATENAME and try the following code (assuming that your masterlist is in a txt file on your desktop, with a state name per line.).

with open(r"C:\Users\Boulot\Desktop\test.txt",'r') as f :
    states
= [name.rstrip().lower() for name in f]

if value.lower() in states:
   
return value


--------------------------

Yogesh Biyani

unread,
Aug 4, 2017, 8:52:00 AM8/4/17
to openr...@googlegroups.com
Hello Ettore,

Thanks for the reply. I will give it a try.

Regards,
Yogesh

--
You received this message because you are subscribed to a topic in the Google Groups "OpenRefine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openrefine/Q5q0KTaB9X8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openrefine+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Owen Stephens

unread,
Aug 15, 2017, 3:51:58 PM8/15/17
to OpenRefine
I'm not 100% sure if this is what you are looking for, but the "GOKb Utilities extension" includes an 'inArray' function which allows you to check if a value appears in an array. You can download the extension from https://github.com/ostephens/refine-gokbutils/ - to install in OpenRefine, download the zip file (https://github.com/ostephens/refine-gokbutils/archive/master.zip), unzip it, and drop the folder/directory into the "extensions" folder/directory in your OpenRefine install, then restart OpenRefine

Owen
To unsubscribe from this group and all its topics, send an email to openrefine+...@googlegroups.com.

Thad Guidry

unread,
Aug 15, 2017, 6:04:45 PM8/15/17
to openr...@googlegroups.com
We have some basic FIND / REPLACE functions in GREL


If you can get your masterlist (ARRAY list) into a simple STRING form like so, then this GREL expression can work for you.
It will return -1 when it does not find a match

indexOf("[minute,hour,day,year,month]","year")  -->  17 

indexOf("[minute,hour,day,year,month]","second")  -->  -1

indexOf("Alabama,Arkansas,Colorado","Arkansas")  -->  8

indexOf("Alabama,Arkansas,Colorado",cells["Column 1"].value)  -->  8

indexOf("[Alabama,Arkansas,Colorado]",cells["Column 1"].value) -->  9

-Thad
Reply all
Reply to author
Forward
0 new messages