473,719 Members | 2,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Concatenate string and variable to make up a constant name

Hi,

I know that it is possible to build up a variable name based on a
string and another variable name i.e.

$varnum=5;
${"varnumber_$v arnum"} = 'This variable should be called varnumber_5';

but is it possible to do this for a constant? For example I may have
constants set up as

define('CONSTNU MBER_1', 'This is constantnumber 1');
define('CONSTNU MBER_2', 'This is constantnumber 2');
define('CONSTNU MBER_3', 'This is constantnumber 3');

and I may want to access them from inside a loop i.e.

for($counter=1; $counter<=3; $counter}}) {
echo 'Contents of current constant = ' . ${"CONSTNUMBER_ $counter"} ;
}

Now obviously this doesn't work because it sees
${"CONSTNUMBER_ $counter"} as a variable but if I try and take the $
off then I just get errors.

Can anyone help? Does this even make sense? :-)

Sep 6 '06 #1
1 14808

Peter wrote:
Hi,

I know that it is possible to build up a variable name based on a
string and another variable name i.e.

$varnum=5;
${"varnumber_$v arnum"} = 'This variable should be called varnumber_5';

but is it possible to do this for a constant? For example I may have
constants set up as

define('CONSTNU MBER_1', 'This is constantnumber 1');
define('CONSTNU MBER_2', 'This is constantnumber 2');
define('CONSTNU MBER_3', 'This is constantnumber 3');

and I may want to access them from inside a loop i.e.

for($counter=1; $counter<=3; $counter}}) {
echo 'Contents of current constant = ' . ${"CONSTNUMBER_ $counter"} ;
}

Now obviously this doesn't work because it sees
${"CONSTNUMBER_ $counter"} as a variable but if I try and take the $
off then I just get errors.

Can anyone help? Does this even make sense? :-)
Typically I've just found the answer. I just build up a string of the
constant name and then wrap the constant function around it.

Obviously I was overcomplicatin g things. Ah well.

Sep 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
22848
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely. This means I need to append trailing spaces to a text string (such as "Test1 ") or append leading space in front of a text string that contains a number (such as " 12.00"). Now, the stored procedure works fine when I run it in Query Analyzer....
3
10541
by: Lars Tackmann | last post by:
Hi - I need a function to concatenate a variable number of strings. I have two ideas but cannot deside which way to go. 1) use the "stdarg" macros - if i use these macros it will be easy to step through the strings, but as I see it (and I may be wrong here)I need to pass the number of strings to the function - which is difficult in this context. 2) using a pointer array as argument to the function. Here the length is again a problem -...
35
5821
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a collective brainstorming session. Let's discuss some C here, for a change :-)
5
2806
by: Generic Usenet Account | last post by:
I have been to recreate a problem that I am having with strings with the trivial code snippet given below. In the trivial code example, I am reading five lines from a data file, each line having only one word. I try to concatenate each word read into a string variable, separated by a delimiter character. Even though I am able to read the words from the file correctly, I am not able to correctly concatenate the string. I was expecting the...
232
13289
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
4
3432
by: kungfuelmosan | last post by:
Hey guys, Im just getting into c++ at the moment so please bare with me Basically i need to declare a vector<stringstringArray(50) inside a class, however by doing so i am getting the following error: error: expected identifier before numeric constant error: expected ';' or '...' before numeric constant But i can declare vector<stringtestArray(50); inside my main
13
21840
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that will concatenate the value of ABC to MYSTR . I need this at compile time.
10
7442
by: Aaron Hoffman | last post by:
Hello, I'm hoping someone might be able to offer some guidance to my problem. I have one query in MS Access which consists of 2 tables joined by a SEQUENCE_ID. By joining the two tables I am able to produce a query that gives me two fields, Part_Number and Product_Type. Below you can see some sample data: Part_Number Product_Type 10MC35231 XYZ1A
1
2421
by: mohanprasadgutta | last post by:
hello everyone, i have to write a program which will take a command line arguement which is nothing but a constant name. in the program i am trying to print out the value corresponding to the constant name provided. i am aware of converting a string to a variable name as follows. my $name = "mohan"; my $var = "name"; my $new = $$name; print "your name is : " . $name . "\n"; But here i have to get the constant value corresponding to...
0
8833
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9365
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5972
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4482
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.