0

I want to learn how to add spaces in variable names.

I know that a lot languages prevent me from doing this, but I believe that there is a trick to do this because I saw someone did it in MQL5

A MetaTrader Terminal allows to show a UI-Dialogue Panel for MMI-assisted setting values for input and extern variables declared in { Expert Advisor | Technical Indicator | Script } code, during a code-execution launch.

( Ref. a picture below ): A MetaTrader Termina Dialogue Panel for setting values for <code>input</code> and <code>extern</code> variables on { Expert Advisor | Technical Indicator | Script } code-execution launch.

user3666197
  • 1
  • 6
  • 50
  • 92
Yassin Mokni
  • 354
  • 1
  • 6
  • 16

6 Answers6

2

In C++ you can't put spaces in variable names but you can get what you want using a std::map.

For example:

#include <map>
#include <string>

int main()
{
    std::map<std::string, std::string> vars;

    vars["Time Frame"] = "15 minutes";
    vars["Indicator Period"] = "16";
    // ... etc

}

The std::map is an associative container that maps one std::string onto another.

Depending on how you intend to use the map you may also want to consider using an std::unordered_map which should have higher performance but will not keep the keys sorted and may have a higher memory usage.

Galik
  • 47,303
  • 4
  • 80
  • 117
  • unordered_map is more efficient in this case – Brahim May 25 '15 at 13:23
  • 1
    @Brahim I'd say "more scalable" – KABoissonneault May 25 '15 at 13:27
  • Can you explain? I meant more efficient in term of performance. – Brahim May 25 '15 at 13:28
  • 1
    @Brahim There's no guarantee without knowing the implementation of std::hash that std::unordered_map is more efficient than std::map for a certain X amount of strings. Maybe the std::hash is actually very long to compute and that it would be faster to compare for equality a couple more times. But with more and more keys, the hash is usually faster, yes. Hence "more scalable". – KABoissonneault May 25 '15 at 13:43
  • @Brahim: That's a dangerous sweeping generalisation. There is no container that is universally "more efficient" than some other container. Each has pros and cons for distinct use cases. That's why we have more than one choice in the first place, after all... – Lightness Races in Orbit May 25 '15 at 14:03
  • @Brahim I have added a note regarding `std::unordered_map` so people can investigate both options. – Galik May 25 '15 at 14:03
  • @KABoissonneault and Lightness Thanks for the comment. What Imeant is that operations in tree-like maps is logarithmic (of number of nodes in the tree) while hash tables operations are done in constant time (of the number of elements in the table). But in practical point of view you are both right – Brahim May 26 '15 at 07:45
2

If you really want to be evil you can sometimes use the left-to-right mark, U+200E which looks like a regular space but is generally not considered whitespace. Different languages and/or specific platforms may behave differently.

This trick seems to work in C# and apparently you can do similar things in ruby.

I tried this using g++ and luckily for everyone's sanity it is not allowed:

foo.cc:5:10: error: non-ASCII characters are not allowed outside of literals and identifiers
    int a<U+200E> b = 3;

Please don't do this outside of pranks and April fool's day jokes.

Eric Appelt
  • 2,843
  • 15
  • 20
1

As much as I know, there isn't any option to add spaces to variables name.

Ido Naveh
  • 2,442
  • 3
  • 26
  • 57
1

The trouble with using spaces in names (whether filenames, variable names or something else) is that you need to have some other mechanism for determining what is part of this name and what is part of the next section of code. Your sample looks like a form, so that has it's own formatting and structure.

SQL does allow you to "quote" variable names with either [name with space] or with backticks `name with space`.

Most other languages do not allow spaces in variable names, because any whitedspace is considered a separator for different lexical unit [different name/word/variable]. There is no way you can change this, as it would alter the meaning of "normal code". Most languages do allow/use _ as a "space in names" character.

Of course, if you have "variables" that are your own construct, read from for example a database or file, you can use your own syntax, and use for example std::map<std::string, sometype> or std::unordered_map<std::string, sometype> to connect the string from your data to the corresponding value.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

Spaces (white space) are used in C++ to isolate keywords and variable names, thus they cannot exist in a variable name or the compiler will treat the text as multiple identifiers.

Example - valid: static const unsigned int my_variable = 6U;

If there is a space between my and variable how does the compiler know which is the variable name? If there are two variables here, it doesn't make sense.

Also, as you can see, there may be more than one keyword in a statement.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

I find a solution .
In Mql5 , when you add a comment next to the variable name , it will appear instead of the variable name . See this image : http://prntscr.com/79vaae

Yassin Mokni
  • 354
  • 1
  • 6
  • 16
  • The StackOverflow purity dictates to carefully separate what is a solution and what is not. **Yes, `MQL4/MQL5` syntactical side-effect allows to display `// a remark text with spaces` in a `GUI_DialoguePANEL` _BUT_DO_NOTICE_ that it does _NOT_ answer the given qeustion _AND_ it does _NOT_ prove a possibility to "inject" space(s) into any variable name** ( which is documented as principally ruled-out as a forbidden feature for the MetaLang syntax parser/compiler ). **Welcome TheXLearner to the world of StackOverflow and both enjoy and contribute to the immense knowledge base here** – user3666197 Jul 23 '15 at 09:40