Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:29:01+00:00 2026-05-24T23:29:01+00:00

I’m writing a function that loops through some info on a registration page. Within

  • 0

I’m writing a function that loops through some info on a registration page. Within the loop I’m trying to call functions based on an array. What I’m having problems with is actually calling the functions properly, because I’m trying to incorporate a variable as part of the function name.

Here’s my code:

<cfscript>
fields = arraynew(1);
fields[1] = 'r_email';
fields[2] = 'r_uname';
fields[3] = 'r_pass';

for(i = 1; i lte arraylen(fields); i = i + 1)
{
    func = fields[i].split('r_');
    func = 'validate_#func[2]#(#fields[i]#)';
}
</cfscript>

So, I have three functions: validate_email, validate_uname, validate_pass. If I throw in a writeoutput(), and attempt to output the results of the function, they do not work.

Here’s that code:

<cfscript>
fields = arraynew(1);
fields[1] = 'r_email';
fields[2] = 'r_uname';
fields[3] = 'r_pass';

for(i = 1; i lte arraylen(fields); i = i + 1)
{
    func = fields[i].split('r_');
    func = 'validate_#func[2]#(#fields[i]#)';
    writeoutput('#func#');
}
</cfscript>

Now, I understand that when you’re using writeoutput(), and you’re calling a function, you need the hash symbol on either end. So, let’s say I write it like this:

writeoutput('#validate_#func[2]#(#fields[i]#)#');

It won’t work, because the second hash symbol cancels out the function call. This is how the function should ultimately look (email example):

writeoutput('#validate_email('email@site.com')#');

How can I replace ’email’ (#validate_email…) with the proper variable name, and still have the function work? I hope I’ve made this understandable!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:29:02+00:00Added an answer on May 24, 2026 at 11:29 pm

    Functions are variables too, so in the same way you can use bracket notation for arrays, structs, and scopes, you can use this to access dynamic variable names (and thus dynamic function names)

    For example:

    <cfloop index="Field" list="email,uname,pass">
        <cfset Result = Variables['validate_'&Field]( Variables['r_'&Field] ) />
        ...
    </cfloop>
    

    Well… not quite. Due to a bug in Adobe ColdFusion, that doesn’t work like that (though it does in other CFML engines, like Railo), and you have to split it into two lines, like this:

    <cfloop index="Field" list="email,uname,pass">
        <cfset TmpFunc = Variables['validate_'&Field] />
        <cfset Result = TmpFunc( Variables['r_'&Field] ) />
        ...
    </cfloop>
    

    (This assumes both function and fields are in the variables scope, if they’re not you need to refer to whichever scope they’re in.)

    This method does have an issue if the function was in an object with state, it loses reference to those variables.

    On CF10, there is the invoke function. Earlier versions of CF need to use the cfinvoke tag.

    (As a side note, CF10 did add the inverse ability of referencing function results with bracket notation, i.e. doSomething()[key] which comes in useful at times.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.