Jump to content

Concatenating Strings And Floats


cspears2002

Recommended Posts

My instructor showed me an expression:

bbox(strcat("../xform_move_me_", atof(opdigits(".")-1)), D_XMAX)

My confusion stems from the following:

/ -> exhelp opdigits

float opdigits (string name)

/ -> exhelp atof

float atof (string source)

Will forcibly convert a string into a float

According to exhelp, both functions return floats. In

just about every programming or scripting language I

know of, concatenating a string and a float results in

an error. Why doesn't the bbox expression above cause

some sort of error?

Link to comment
Share on other sites

My instructor showed me an expression:

bbox(strcat("../xform_move_me_", atof(opdigits(".")-1)), D_XMAX)

My confusion stems from the following:

/ -> exhelp opdigits

float opdigits (string name)

/ -> exhelp atof

float atof (string source)

Will forcibly convert a string into a float

According to exhelp, both functions return floats. In

just about every programming or scripting language I

know of, concatenating a string and a float results in

an error. Why doesn't the bbox expression above cause

some sort of error?

Actually there are plenty of languages out there that do implicit type conversions based on context which is what hscript is doing here. strcat needs strings as arguments so if it sees integers or floats it simply converts them for you. Perl is a good example of a language where implicit type conversions like this are common. For example, in Perl you can have:

$string = "Hello"			  # Quotes denote strings
$int = 42					  # No quotes denote integers or floats
print $string . $int	  
> Hello42

Link to comment
Share on other sites

My instructor showed me an expression:

bbox(strcat("../xform_move_me_", atof(opdigits(".")-1)), D_XMAX)

My confusion stems from the following:

/ -> exhelp opdigits

float opdigits (string name)

/ -> exhelp atof

float atof (string source)

Will forcibly convert a string into a float

According to exhelp, both functions return floats. In

just about every programming or scripting language I

know of, concatenating a string and a float results in

an error. Why doesn't the bbox expression above cause

some sort of error?

As grasshopper already noted, the "str" expressions convert the arguments on the fly to a string type. For example, if you did:

strcat("foo","1")

strcat("foo",1)

strcat(foo,1)

strcat(foo,"1")

the result is always foo1.

What I didn't understand in your expression was the section atof(opdigit(".")-1) . You are right that opdigit returns a float so I'm not sure why you'd put the atof before it. It still evaluates the same if you remove the atof.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...