Chapter 2 Page 55
by Jason Cadden · in Torque Game Engine · 07/07/2005 (4:50 pm) · 5 replies
I am very new to programming. I just picked up Mr. Finney's book two days ago. On page 55 he goes through a program that displays the price of some fruits and total cost. I had a question about some of the lines of code.
print("Cost of Bananas(ea.):$"@$bananaCost);
print("Cost of Apples(ea.):$"@$appleCost);
print("Number of Bananas:"@$numBananas);
print("Number of Apples:"@$numApples);
I would like to know why the first two lines have a $ after the colon, and the second two lines do not. The first two lines go $"@", while the second two only have "@$. Hopefully I've made this clear enough.
print("Cost of Bananas(ea.):$"@$bananaCost);
print("Cost of Apples(ea.):$"@$appleCost);
print("Number of Bananas:"@$numBananas);
print("Number of Apples:"@$numApples);
I would like to know why the first two lines have a $ after the colon, and the second two lines do not. The first two lines go $"@", while the second two only have "@$. Hopefully I've made this clear enough.
#2
"Cost of Bananas(ea.):$"
and another
"(BananaPrice)" that stored in global variable $bananaCost
Read the book you will find explanation later in it.
Good Luck!
07/07/2005 (5:05 pm)
So in first example we have one string"Cost of Bananas(ea.):$"
and another
"(BananaPrice)" that stored in global variable $bananaCost
Read the book you will find explanation later in it.
Good Luck!
#3
07/07/2005 (5:11 pm)
I get it. Yeah, it's pretty simple once I actually took a look at it. Basically it's printing the $ on the screen in the first two lines since it's a dollar amount. In the last two lines, it's just saying how many total pieces of whatever there are, not any price so the $ isn't needed. Thanks for the help.
#4
I've not run this code myself, but from looking at it, the result would be
Cost of Bananas(ea.):$12
Cost of Apples(ea.):$17
Number of Bananas:7
Number of Apples:3
In the first two lines, the dollar sign is only used as a literal string.. something that will be printed and is eactly the same as any of the other characters. The example is confusing and I can see why if you're new to programming you are confused. But don't worry.
Basically, the print command is layed out like this
print( some expression );
an expression can be anything including text in quotes or variables or any combination concatinated together.
print("Hello there");
will print
Hello there
print("Blah :$");
will print
Blah :$
If you have a variable called $jase and you set it to 5 $jase = 5
and you say
print("What is $jase?"@$jase );
you will get
What is $jase?5
So basically, the @ symbol attaches the value to the end of the string. Concatination. (page 59)
I just tried this code in the console..
$bob=5;
echo("What is bob?"@$bob);
I got
What is bob?5
Hope this helps and I hope I wasn't patronising.. I certainly didn't mean to be.
07/07/2005 (5:13 pm)
Hi Jason,I've not run this code myself, but from looking at it, the result would be
Cost of Bananas(ea.):$12
Cost of Apples(ea.):$17
Number of Bananas:7
Number of Apples:3
In the first two lines, the dollar sign is only used as a literal string.. something that will be printed and is eactly the same as any of the other characters. The example is confusing and I can see why if you're new to programming you are confused. But don't worry.
Basically, the print command is layed out like this
print( some expression );
an expression can be anything including text in quotes or variables or any combination concatinated together.
print("Hello there");
will print
Hello there
print("Blah :$");
will print
Blah :$
If you have a variable called $jase and you set it to 5 $jase = 5
and you say
print("What is $jase?"@$jase );
you will get
What is $jase?5
So basically, the @ symbol attaches the value to the end of the string. Concatination. (page 59)
I just tried this code in the console..
$bob=5;
echo("What is bob?"@$bob);
I got
What is bob?5
Hope this helps and I hope I wasn't patronising.. I certainly didn't mean to be.
#5
someone already pointed out what the @ operater is for.
07/08/2005 (7:00 am)
Simple answer to the question is that the $ you are asking about is a dollar sign. it's inside the quotes so it doesn't stand for any kinf of variable information, it's just a dollar sign that will be printed as such. someone already pointed out what the @ operater is for.
Torque Owner Viktor Kuropyatnik
@ - this is string concatenation operator in Torque script
so
print("Cost of Bananas(ea.):$"@$bananaCost);
will look like:
Cost of Bananas(ea.):$100 (if banana cost so much :) )
the third line
print("Number of Bananas:"@$numBananas);
will look like:
Cost of Bananas(ea.):20
in Torque script $ in variable name means that this variable "global"
and % mean that it is "local"