Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

ARRAY Search [RUBY] ??

For general discussion related FlowStone

Re: ARRAY Search [RUBY] ??

Postby RJHollins » Mon Nov 18, 2013 6:54 am

Hey ... Thanks Tronic !

This '<<' command in Ruby is very interesting. It seems to be able to stuff new entries into the array.

I'm more use to seeing array's expanded with a counting INDEX ... but this '<<' is such a different technique.

Thanks !
8-)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: ARRAY Search [RUBY] ??

Postby RJHollins » Thu Nov 28, 2013 12:36 pm

Got a 'speed' question here ...

With the design logic of this Ruby search routine, I'm curious if the 'length of the search string' has an impact on the execution speed.

IOW ... if, for example, the search is to find 1 specific entry ... and this entry is unique enough to be located with only a partial search string ... is there any difference in result [speed-wise] if I use just a partial string to find it, OR if I used the entire name for the search string ?? <does that make sense> ???!!! :lol:


on a related topic ... is there a 'timing' module of sorts that would help time analyze the execution speed of a process we put together ?? As a way to judge the efficiency of a Ruby code, or even to compare that to something in 'green' ? I remember when I played with a basic language [Atari 1040STe] that I could time a routine to see how fast a piece of code would execute. It gave me some benchmark reference to test performance ... man that was a long time ago :shock:

thanks !!
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: ARRAY Search [RUBY] ??

Postby trogluddite » Mon Dec 02, 2013 3:16 pm

Generally speaking, a Regular expression search will return as soon as it has found a match - so if, for example, the search term is very close to the string start, it will return quickly, even if the same search term occurs elsewhere in a very long string.

The rest depends on how rigid the search is...
The regular expression /dog/ searches for the word 'dog' anywhere in the test string - so for a non-matching case, the whole search string has to be parsed.
/d[ou]g/ searches for either 'dog' or 'dug' - so parsing will take a little longer because of the optional characters.
/d\.*g/ searches for any quantity of any character between a 'd' and a 'g'. This kind of thing will take ages to search, because there are just so many options available. But, worse still, 'any character' includes 'd' and 'g' - so there could be a lot of ambiguous cases - ..."good dogs don't go" ... between which 'd' and which 'g' did you mean, exactly?.

So writing regular expressions is a bit of a black art - when there are lots of wild cards, there are certain pathological cases that will take an age to return a value if the search string is long. Understanding the reg' exp' anchor codes is key to reducing ambiguity, e.g...
/\Adog\b/ - find 'dog' only if it is at the string start and followed by white space.

In general though, given the same level of complexity, regular expressions should be very fast compared to doing your own parsing on strings - the Ruby methods pass the input data to a regexp engine made using compiled C code
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: ARRAY Search [RUBY] ??

Postby RJHollins » Fri Dec 06, 2013 8:26 am

mmm ... learned some new things here. Thanks TROG !

Well, I should say, I've just read some new things regarding wildcard string and searching. :lol:

It helps to be exposed to some of the possibilities that's for sure 8-)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: ARRAY Search [RUBY] ??

Postby RJHollins » Mon Dec 23, 2013 5:01 am

Still exploring more needs of the search routine.

Was wondering ... can a 'Regular expression search' be limited to find only an exact match ???

I have a 2-d array that I need to maintain relationship ... but I'm finding it difficult to 'find' only an exact match.

i.e.: searching through a list might have 15, 5, 35, 55. If I search for 5, I get all of these [which makes sense, but doesn't get me the result I need.

How can I limit the 'Regular expression search' to only find the entry 5 ??

This is from the 'Trumpet Find routine'
Code: Select all
# NB) Assumes that all patch numbers are unique

#@names = ['Trombone','Solo Trumper','Wind Chimes',"Bassoon","Bass Guitar"]
#@progs = [5,11,32,205,41]

@names=@in
#@progs=@in

# Turn the arrays into a hash keyed by patch number.
# Hash is better for sparse data where every nuerical index might
# not have a corresponding value.

@patches = Hash.new

@progs.each_with_index do |prog, index|
  @patches[prog] = @names[index]
end

# Now @patches[number] will retreive the name of the given patch

# Search term
#@search = "tR"

# An array to return the search results
@found = []

# Do the search
# Go through each hash item, passing each key and value in turn...
@patches.each do |number, name|

  # Use a regular expression to test for a match - the characters
  # then don't even have to be at the string start.  The 'i' at the end
  # makes it case <i>nsensitive!
 
regexp = /#{@search}/i
   
# regexp = /#{@search}\z/i  # exact match only
#  regexp = /#{@search}/\a\z  # exact match only
 
 
  # Put matching items into the found array
#  @found << [number, name] if regexp.match(name)

@found << [name,number] if regexp.match(name)
end

@found  # Each item is a sub-array [program, name]

#output 0, @found

@found.join(",")

output 0,@found.join(",")
# sorted
output 0,@found.sort.join(",")

watch @found.sort.join(","), @found.join(",")


I've left some of my 'experiments' in there just to add to the confusion :|

Thanks for any insights ;)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: ARRAY Search [RUBY] ??

Postby trogluddite » Mon Dec 23, 2013 8:23 pm

Hi RJ,

To check for a whole string match, you need to use the '\A' and '\Z' markers for 'start of string' and 'end of string' respectively. They go into the Regexp in the position you want them to be tested - so at the start and end for these...
Code: Select all
regexp = /\A#{search}\Z/i

PS) When doing this, it is often a good idea to use ''String.strip' on the search terms - this removes any leading and trailing whitespace, including stuff you can't see like new-line characters that might otherwise mess up the search.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: ARRAY Search [RUBY] ??

Postby RJHollins » Mon Dec 23, 2013 9:26 pm

Hi TROG !

I've just been reading and experimenting with 'word boundaries' which is looking like:
regexp = /\b#{@search}\b/


This has been 'partially' successful, but maybe too tight :shock:

Will definately try the example you just posted !

THANKS! :D

... edit ...

OK ... it looks like
Code: Select all
regexp = /\A#{@search}/i

is the solution :o
:D

I need to test this more, but it looks like I'm now getting the various results at the output.

This Ruby stuff is such a great tool ... but man, there is so much to learn ... but worth it ! :lol:

Thanks for putting me on the right track TROG ... especially the comments you provide that help to get a better understanding. I can't tell you how many RUBY sites I've searched [all helpful of course].

Need to really test this to be sure everything is checking out correctly.

Again ... Thank-You TROG ! :D
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Previous

Return to General

Who is online

Users browsing this forum: No registered users and 71 guests