Variable variables

Are there variable variables in Qt? I googled a little bit and it seems like NO, but maybe I googled badly.

So, I asking about an analog of PHP's

$$varname = 5;

and about analog of this notation

$a{$varname} = 5;

or simple a{$varname}

Just interesting. Thanks.

Comments

  • "variable variables"? Do you mean like a C pointer, i.e. a variable that can indirectly refer to another value? It is not Qt that has to have them, but rather ECMAScript.

    If so, I think it does, "references", but only for objects. You can wrap an object around whatever values you want, though. Verify this as I really don't know what I'm talking about, just something I think I remember from the dozen or so lines of Javascript I've ever actually written :)

  • Victor_BVictor_B Posts: 391
    edited December 2020

    I don't know C and don't used that in JS, but in PHP time to time. To be clear:

    $varname = 'alibaba';$$varname = 5;print($alibaba); // 5$state_5 = 'Nevada';$num = 5;$state = ${"state_$num"};print($state); // Nevada

     

    Ok, forget this... Seems like, only PHP can do this bullshit magic :))

    Post edited by Victor_B on
  • Rob offers:

    (function(){		var nNum = 5;	var oStates = {};		oStates.nevada = {};	oStates.nevada.name = "Nevada";	oStates.nevada.num = 5;		//print( oStates.nevada.name ); //Nevada	//print( oStates["nevada"].name ); //Nevada	//print( oStates["nevada"]["name"] ); //Nevada	//print( oStates.nevada.num ); //5	//print( oStates["nevada"].num ); //5	//print( oStates["nevada"]["num"] ); //5		oStates[ "state_" + nNum ] = {};	oStates[ "state_" + nNum ].name = "Nevada";		//print( oStates[ "state_" + nNum ].name ); //Nevada		print( JSON.stringify( oStates, null, "\t" ) );	})();

     

  • SimonJMSimonJM Posts: 5,982

    I used to call this 'macro substitution' as I first came across it in dBase II back in the 80s and that was what it was called. It's a useful feature but it can get a bit confusing!

  • Victor_BVictor_B Posts: 391
    edited December 2020

    SimonJM said:

    I used to call this 'macro substitution' as I first came across it in dBase II back in the 80s and that was what it was called. It's a useful feature but it can get a bit confusing!

    Curly braces? Yes, it's a substitution. But $$ is "variable variables" in PHP. Confusing? Only if you use it like this:

    $Bar = "a";$Foo = "Bar";$World = "Foo";$Hello = "World";$a = "Hello";print( $a ); // Helloprint( $$a ); // Worldprint( $$$a ); // Fooprint( $$$$a ); // Barprint( $$$$$a ); // aprint( $$$$$$a ); // Helloprint( $$$$$$$a ); // World

    laughlaughlaugh

    Yep, I can put my "variable variables" into array... Thanks Richard and Rob.

    Post edited by Victor_B on
Sign In or Register to comment.