• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Dynamic Variable Names from an Array

Guest
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

Hello everyone.
I am working on a semi complex project that requires me to be able to create variables on the fly. Basically I have an array, the second element contains the name of the variable, and the 3rd element contains the data. I wanted to do something like what I have posted below, but it's not working. I should get fields named like form.a, form.b, form.c and so on, each with some data assigned, but when dumped, the form variables remain empty.Like form.finalarray[1][2] = "#finalArray[1][3]#" should come out to be an form.a = c, but when form.a is dumped, it is empty. Why doesn't the block of code below work?

TOPICS
Advanced techniques

Views

481

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

You'll want to use the syntax:
<cfset form[finalarray[1][2]] = "#finalArray[1][3]#">
to correctly set the form variable

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

FORM is a basically ColdFusion structure, form field values as structure keys.

You can only pass simple values in form fields. Thus it makes no sense being able to have arrays as form field variables. Thus, I don't see why your code should work...

If you create a myStruct structure and replace "form." with "mystruct.", does your code work then?

Btw, you gain nothing by using "#finalArray[1][3]#", compared to finalArray[1][3]. Don't use # signs or quotes in your code example, they are bad coding practice.

Edited: And yes ,see the answer above this reply. There the form key value is actually evaluated, because the key name finalArray is wrapped within the []'s. It's the same as form['#finalArray#'] if that makes it clearer to you. But again,ditch the # and "s.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

> Basically I have an array, the second element contains
> the name of the variable, and the 3rd element contains the data.

What does the FIRST element contain?

And how come you're not using an array of structures, as there seems to be
more a key/value relationship here, rather than an indexed one, which
suggests some numeric signficance in the order of the elements (which there
isn't, between "name" and "value" which seems to be your siutation).

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

Thank you all so much for your replies. I know this peice of code looks insane and like it serves no prupose, but trust me it does. The first element of the array conaints a number, that frequency of how many times the letter in element 2 occures in a block of text in the form. For those interested, the project I am working on is a tool that will help the cracking of monoalphebetic substitution ciphers using frequency analysis. That array is what holds the frequencies of every letter in the block of text given. It gives the freq in the first element, the actual letter in the second element, and the letter it probably originally was in the 3rd element. I am trying to fill in some form fields with the data from the array. Again, I know this all sounds insane, but there is method to the madmess. If you care to look, this project in progress can be found at http://www.digitalswordsmen.com/Codebreaker.cfm. Its free to fool around with. Lots of diagnostic info is displayed, and the source is downloadable if you want to check it out. Thanks again for all your help, Ill let you know how it goes.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

> It gives the freq in
> the first element, the actual letter in the second element, and the letter it
> probably originally was in the 3rd element.

Right, so you're describing a struct, not an array. There is no
*sequential* relationship between "freq", "cipher letter", and "probable
letter", is there? So an indexed array is not the most sensible/obvious
data construct to store the data in.

What's more, you've got uniform key names, right there:

stChar.freq
stChar.cipher
stChar.actual

Although as you have a collection of them, rejog that slightly:

stChars[cipher].freq
stChars[cipher].actual

(given the cipher chars would be unique, I should think, in your system).

Did you suss out your problem with the AAAAAA thing, from last night? I
suspect I saw what the problem was, but wondered if a bit of
experimentation would lead you to the same conclusion I did... [plus it was
late when I saw the thread, and I'd want to run some test code to check my
theory, and I... well... couldn't be bothered, to be honest. Oops].

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

Ah. Yeah. See I am a really terrible programmer, and I don't even know the difference between a structure and array. I don't even know how I have made as much as I have. The AAAAA problem is partially fixed, sometimes it still freaks out and puts a bunch of one letter for no good reason that I can think of. If you know why, please let me hear it. I wish i was better at coding.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

Oh yeah also that fix provided worked for my dynamic vars. Now I just need to save them back to the form for the replacement alphabet. So thanks for that.

Well After some work, I got it going. It puts the correct values in the correct form boxes. However my replace statements are messed up. I think its because one replace statement is change another. I need some way to do them all at the same time. Basically I just want to change every letter in "Text" to whatever values the user has specified in the form. So whatever they put for Form.A should replace any A that occurs in the text. Attached is the code for my replace statements.

This image here http://www.digitalswordsmen.com/img/cipher_tool_output.gif Really shows what is going on better than i can explain it. Everything is all set up right, but the results of the replace are completely unpredictable.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

> The AAAAA problem is partially fixed, sometimes it still freaks
> out and puts a bunch of one letter for no good reason that I can think of.

Let's take a smaller sample, in which the alphabet is only three
characters, ABC.

Your cipher mapping is:
C->A
A->B
B->C

Now, decipher "CAB":

Change the Cs to As:
AAB

Change the As to Bs:
BBB

Change the Bs to Cs:
CCC

That approach is never going to work, is it? You're always going to
accidentally decipher some letters more than once.

> If
> you know why, please let me hear it. I wish i was better at coding.

This ia more a debugging issue. Had you ever actually manually followed
your logic through (with pen and paper, if need be, to track results),
you'd've seen this. When something is wrong, don't sit there going "it
SEEMS right, I can't see it", have the point of view: "it's clearly
wrong... why?".

All this comes with practice.

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

> That approach is never going to work, is it? You're always going to
> accidentally decipher some letters more than once.

If it's any consolation, CF falls into the same trap:

<cfoutput>#replaceList("cab", "c,a,b", "a,b,c")#</cfoutput>

(outputs "ccc").

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 05, 2007 Feb 05, 2007

Copy link to clipboard

Copied

LATEST
Ha, I found a solution. I use a case sensitive replacement. Before replacement I set the variable to uppercase, then I replace with lowercase letters. That way nothing gets replaced more than once, because it will always change case! It seems to work perfectly. The whole thing works now, if you get a change you should check it out. Thanks for everyones help. If you care to try for yourself, here is the code. It's not to shabby if I do say so myself.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation