Page 1 of 3

ARRAY Search [RUBY] ??

Posted: Tue Aug 13, 2013 3:38 am
by RJHollins
Wow, was hoping that I was getting a little more handle with RUBY ... apparently, 'almost' is still only good in horseshoes :roll:

Have made 'some' progress ... like parsing down an XML file that contains 'patch presets' for programs I'm working on. With the latest discovery of these files, it now seems possible that I can use this XML file to create all the patch names and PRG numbers I need, instead of manually assembling them :o

I'm experimenting with some ideas to see what the better course to take in doing this.

I have a 'working' module that dissects the XML file into 2 separate arrays. One is the 'patch name', the other is its' 'patch number' [MIDI].

I need to get this full list [maybe 100 or so], organized into related groups [by name], and maintain a reference to its patch number. These 'groups' will feed a 'pull down menu' selector, allowing the User to select a patch from a specific 'bank'.

I was thinking that a 'Search Array' routine would be helpful in putting these groups together. Which has, thus-ly, led to much brain abuse :lol:

I've been going through RUBY sites trying to get a handle on this ... only to ALMOST get it working .... or completely NOT working with errors messages that I think RUBY is starting to make up :shock: it's ridiculous I tell ya :roll:

I thought that a 'Search' routine that would find ANY character in the array list would output the full name it was contained in [along with its index number]. As more characters are added, the output list would narrow down to the prime candidates. But man, I've not gotten anything working [Ruby wise]. I did find a 'Green' module that kinda works, but it is not finding/matching with only part of a string .... it basically needs the full name ... and it is CASE sensitive, which adds to the problem.

I think I can do this with a single dimension array ... as the dual attempt really got nowhere [for my attempts].

Any bones that could be tossed this way to help would be most appreciated ! I'm going to try and reduce my 'Xperiment Schematic' to something manageable and readable ... but right now it's a conglomerate of all type of trials ... not pretty :oops:

off we go .... :roll:

Re: ARRAY Search [RUBY] ??

Posted: Tue Aug 13, 2013 10:12 am
by Nubeat7
maybe something like this?

Re: ARRAY Search [RUBY] ??

Posted: Tue Aug 13, 2013 10:25 am
by Nubeat7
RJHollins wrote:
I need to get this full list [maybe 100 or so], organized into related groups [by name], and maintain a reference to its patch number. These 'groups' will feed a 'pull down menu' selector, allowing the User to select a patch from a specific 'bank'.


hmm maybe hashtables would be of interest for that,
http://julioterra.com/journal/2011/10/l ... sh-tables/
http://ruby.about.com/od/rubyfeatures/a/hashes.htm
but i`m not very familiar with hashtables,

Re: ARRAY Search [RUBY] ??

Posted: Tue Aug 13, 2013 5:46 pm
by RJHollins
Nubeat7 wrote:maybe something like this?


I mean .... that one line does what about a dozen Prims take :|
:lol:

I was trying things like RUBY's find_all, or grep commands. [and probably several others I think] :roll:

I also see my additional confusion .... what the difference between @in and @ins is ??? :?

Anyway, this core search works Nubeat .... Thank-you!

I'm still will need to figure out outputting the related INDEX number to the found string.
Also, I will need to make it NOT 'case sensitive' on the search.

Oh ... a question ... in the code ' |s| s.include? ' ... what does the variable 's' represent ??

Thanks again Nubeat ... I have so much very basics still to understand. :oops:

Re: ARRAY Search [RUBY] ??

Posted: Wed Aug 14, 2013 9:01 am
by RJHollins
I've tried to tap into NuBeat's code to also include an INDEX number of all found strings in the Array ... but no success :cry:

The only way I could do this is:

Code: Select all

array = @in 
array.index @search

output 0, array[(array.index @search)] 
output 1, (array.index @search)


However, this version only finds an exact match. It is also case sensitive. During the enter of search string I see error message in the Ruby interpreter.

I just can't figure out how to include an 'Index' of all strings found [as a separate OUTPUT], as in NuBeat's example.

Then to make it NOT case sensitive.

Still pouring through more Ruby instruction sites, but could really use more help.

Thanks everyone !!

Re: ARRAY Search [RUBY] ??

Posted: Wed Aug 14, 2013 12:20 pm
by TrojakEW
Don't know what exactly you are trying to do but this will search array and return index of ellement you search

Code: Select all

a = ["test1","test2","test3","test4"]
a.index{|s| s.include?("test2")}


so output will be: 1

Re: ARRAY Search [RUBY] ??

Posted: Wed Aug 14, 2013 7:47 pm
by RJHollins
TrojakEW wrote:Don't know what exactly you are trying to do but this will search array and return index of ellement you search

Code: Select all

a = ["test1","test2","test3","test4"]
a.index{|s| s.include?("test2")}


so output will be: 1

Don't know what exactly you are trying to do

I ask myself that often :lol:

What I'm trying to get ...

I have 2 Inputs ... an ARRAY with a list of patch names. The 2nd input is the 'Search' string.

For the Output, I need a list of all Patch names that contain the search string. Exactly as NuBeat's code does.
I also need a separate Array Output of all the Index numbers to the found list.

To this end, I've been trying several Ruby commands ... even looking into 'Hash' ... but have not been able to get this to work. :cry:

With NuBeats search code:

Code: Select all

output @in.select {|s| s.include? @search}


I get the desired output array. But I also need a separate 'INDEX' output of those found.

Getting rid of the 'case sensitive' search I'll try to solve afterward.

As I'm trying to learn, I realize that RUBY is a powerful language. It can sometimes be condensed to very tight coding. Unfortunately, for the amateur, this can make it more difficult to understand or modify. Of course, it is on me to get more experience and understanding. In that pursuit, I sincerely appreciate the knowledge that is shared across the forum.

Thanks !!

Re: ARRAY Search [RUBY] ??

Posted: Wed Aug 14, 2013 7:55 pm
by trogluddite
Keys to solving this, I think, are...

- Get all the information into a single data structure
- Test strings using regular expressions - they take some learning, but are super versatile.

Something like this...
Trumpet Finder.fsm
(849 Bytes) Downloaded 1261 times

Re: ARRAY Search [RUBY] ??

Posted: Wed Aug 14, 2013 9:05 pm
by tester
Welcome back Trog! :mrgreen:

Re: ARRAY Search [RUBY] ??

Posted: Wed Aug 14, 2013 9:43 pm
by TrojakEW
or something like this. it will search for string and return array with index and element from input array

Code: Select all

a = ["preset_test1","test2","preset_test3","test4"]
names = a.select {|x| x =~ /preset/ }
number = a.each_with_index.select { |i, idx| i =~ /preset/}
number.map! { |i| i[1] }
number.zip(names)