Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

extracting a random item from a value list - HOW?


This topic is 7862 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

Hello All

I would like to find a formula that would allow me to take a value list i.e. "Colors" and get random item from that value list and deliver it to a field. Can someone help me with this?

Value list "Colors"

RED

Orange

yellow

green

Blue

Violet

Returns the random of the above list. ( "yellow " for example)

Value list field, "Colorfield"

File - "NewFile"

Please can someone show me a fool proof efficent way to set this up. I would have a lot of uses to do this with value lists.

Thanks again

harty

confused.gif

Posted

Start with:

Position (text, search

Posted

Hi,

My first thoughts would be to do it with a calculation that return a random occurence from your value list :

6 values woud lead to :

c_randomOccurence = Int(Random*5) +1

where 5 is your total occurence Occurence (ValuelistItem(Status(CurrentFileName, "color list")

to return a random occurence from 0 to 5 from your value list :

0 for first occurence, 1 for second,....

Then, the basic calcs to return a given occurence are :

c_return1stOccurence =MIDDLE(

ValueListItems(Status(CurrentFileName, "your color value list",1,POSITION(ValueListItems(Status(CurrentFileName, "your color value list", "

Posted

Does anyone have a working example of extracting a random value from a value list ? I just can't seem to make the examples here work properly.

Thanks

Posted

Hi,

Here is a working implementation. Some FM Gurus here would certainly come with a cleaner solution, but that's what I've got, you Lazy Man grin.gif

(Please tell if you can't download it. I've got some prob with DropZip, but you should open the files with FM when unzipped, even if they don't look as FM files. )

And for those 3 quick guys that already downloaded it, note that I've already modified the random formula without the +1, as 0 can be used here for 1st occurence)

random.fp5.zip

Posted

Hi Pete,

It looks great....Did you tried it ? It always return the last occurence when I use it. Maybe I just got confused when changing the overall syntax to my European FM.

Thanks.

Posted

No it doesn't work - sorry! I forgot I am continually getting different results. One will have to create another field. So, it could be:

RandomField = Truncate((PatternCount((ValueListItems(dbname, valuelist)), "

Posted

Hi Guys

All looks great. Now, lets assume you always want the predefined color field to remain. Ok, thats easy.

But now we want only one more field that provides the solution - and not via a script.

As I see it both the script and the calculation fields require 2 parts (so that there is no multiple randomising happening). How about a 1 part solution?

Posted

Hi,

Sorry Ray I don't get nor Pete request, nor your solution. What"s exactly the point here ?

Pete,

If the random calculation is set as an auto-enter unmodificable, why would you like the record to change ?

Posted

Peter was asking for a way to select a random entry from the value list which requires only a single field and is not dependent on a script.

The revised file I posted uses an auto-entry calculation.

Although I left the script in my example, it is only there to show that the revised formula can work by script as well as via auto-entry calc - but it would only be required if the values were to be revised (randomly) after the record is created.

Posted

Haahhh!!

OK, got it afterwards when revisiting your sample and creating a new record.

And you did it with one field only !!!!!

Ray, you know already what Vaughan would say grin.gif

Posted

Help Me

And here I thought my FM skills were pretty good. I'm blown away! (Although I still quietly hope that interpreting someone elses work is not necessarily very easy)

Ray, I cannot seem to unravel your excellent calculation - how did you create it? When you faced the question what was your first step? I want to gain more insight into the thought process that ultimately leads to such a result. Also, how long did it take you to work out?

Pete

Posted

Hi Peter,

The calculation is not as impenetrable as it first looks.

You asked if the calculation could be achieved in one step. In the previous version a conventional parsing technique locates the start and stop characters using the Position function on the CRs. As you know, it is this procedure that depends on three succesive references to the random number.

In considering other ways to parse the list - and being aware that the MiddleWords( ) function automatically breaks over CRS - I knew that if the value list were to contain only single word entries, it could be parsed using the MiddleWords( ) fucntion.

The list in the demo *is* all single words, as it happens, but I knew that simply using MiddleWords( ) would cause the formula to break as soon as anyone decided to put a two-word item (eg 'dark blue') on the list - ie since MiddleWords( ) breaks over both spaces and CRs, this would be problematic.

The solution was to eliminate the spaces (if any), replacing them with a string which is not likely to be found within any legitimate values, now or in the future, then apply the MiddleWords function to parse the list (now as 'single-word value packets'), and then subsequently reverse the substitution to put the spaces (if any) back in. A bit 'lateral', but feasible and not too onerous.

So the logic of the formula, working from the middle out is:

1. Fetch the valuelist

2. Change the spaces to .spc.

3. generate random number n

4. take out the nth 'word'

5. Change the .spc. strings back to spaces

With the result that single or multiple word value list items are parsed out accurately, yet the procedure depends on a single random number and can be compiled into a composite expression.

For what it's worth that was the process. It took only a short time to write and test - perhaps about half of the time which elapsed between your query and my reply. wink.gif

Posted

You guys are awesome. I have so much to go over with what you did. This is the best way for me to learn . Pull something apart and ask questions later. I especially liked hearing about the thought process. thanks so much all, It is great what happens here.

thanks

Posted

What most people do when they want an exact selection from a value list, is to select it from a pop-up menu or checkbox field (etc).

I am guessing, however, that what you are asking is whether extraction of a predetermined value from a list can be automated? It is difficult to think of many cases there you'd need to do this, because if you can predetermine which value is to be selected, then in most instances you won't need to extract it from the list, but can simply specify the value itself.

Notwithstanding that, if you want the selection to be automated - but not random - then presumably you have in mind some method by which the 'exact' selection will be specified. Presumably it will be specified in either of two ways, you can say which value you want (in which case, as noted above, there is no longer a need to extract it from the list, because you can have a script enter it directly) - or you can spefify the number/position of the value to be selected.

If you want to select a value from the list by number, then various ways to do that have already been discussed on this thread. The earlier examples suggested were all seeking to providing ways to get at a random list item in two steps, where the first step created a random number and the second step found the corresponding list item. It was only the last example that collapsed these two processes together.

Using the approach exemplified in the final demo, you could retrieve the Nth value from a list with a formula along the lines of:

Substitute(MiddleWords(Substitute(ValueListItems(Status(CurrentFileName), "Colors"), " ", ".spc."), N, 1), ".spc.", " ")

...where N is either a number (entered into the formula) or a field via which a variable number is to be supplied.

Posted

Well...

The reason is so that i can do a "Find" for each of a few value lists selected (which in my case are conditional and can't do a find with) and so be extracted to a text box where as it can be searched. Its kind of a one way, what else could i do...

Complicated huh?

Posted

Hi mastichis,

I have a feeling the you are making things more complicated than they need to be.

I suspect that a dynamic list selection system which is dependent upon mode might be preferable.

An example of a set-up that provides this functionlality is attached.

DynamicList.zip

Posted

For anyone following this thread, I have edited the formula cited in my 05/02/03 12:38 AM post (above) to correct a typographical issue (the sequence ") + 1" was included after N erroneously).

For clarity, attached is a file which shows a working version of the corrected formula.

ListExtract.zip

  • 2 weeks later...
Posted

wouldn't it be easier to write a friendly little plug-in to extract a certain line, and to count the number of lines? instead of doing the arcane extraction in FM.

I'm quite sure it is no more than about a page of C code, of which most would be decoding and coding numbers. . The actual productive code can't possibly be more than 15 lines of C.

Getting a certain line, and counting the number of lines is so common it should be built-in. Peter

Posted

To Ray:

your solution idea is very clever. Thanks.

I did not find the definition of "word" in filemakers help, is there one? I found that multiple CR count practically as one separator, therefore it is even more safe to replace CR by .CR . and back.

Posted

I did not find the definition of "word" in filemakers help, is there one?

Yes there is, after a fashion, but it doesn't appear in the help file. Instead, you'll need to pay a visit to the FMI TechInfo Knowledgebase to view it. Yhe URL is:

http://www.filemaker.com/ti/103853.html

And for added insight into how the different non-alphanumeric characters are interpreted by word related functions, it is also worth a visit to the related article:

http://www.filemaker.com/ti/104103.html

Once you have these in your 'kit' it's a lot easier to predict how FMP will behave when an XWords( ) function encounters a given string.

Posted

Ray thanks for the links. I consider bying a plug-in, or writing one, to break up lists, perhaps even doing DDE to a hypercard script.

Do you know if there is a way to convert a repeating field into a value list (short of creating an item database file, one record per list item - and similar constructions)

Peter

This topic is 7862 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.