I cannot find proper documentation in the user guide, but my understanding was that a string is a sequence of characters enclosed in quotation marks, like "1234"
Maybe someone knows: are the quotation marks part of the string?
I ask because programs like this, where one wants to use the output as the input for another program. For example
EXPORT bin(N)
BEGIN
LOCAL B:="";
WHILE N >0 DO
IF FP(N/2)=0 THEN B:=B+"0";
ELSE B:=B+"1";
END;
N:=IP(N/2);
END;
RETURN B;
END;
bin(N) returns the binary expansion of the integer N. In Home or CAS, bin(13) returns "1011" with quotation marks. Replacing RETURN B with PRINT B and running the program, returns 1011 to the output screen, without the quotation marks.
I hope someone knows what is going on here. Thank you.
The PRINT() command prints a string by simply displaying its characters. A string is just a sequence of characters. It does not include the double-quotes.
The double-quote is simply used for display purposes as well as for enabling users to specify that the sequence of characters is a string. Without it, how else would a user be able to distinguish the number 123 vs. the string 123 if all that is shown in the screen is 123? So this is why the calculator will display "123" instead of just 123. The same goes for creating strings, etc.
Thank you. I was thinking more from the calc point of view. If I had another program nib() that inputs a string of 01's and converts it to a string of -101's, how should I do the composite
nib(bin(13)) so that the quotes are not part of the input?
(12-11-2013 07:11 PM)Alberto Candel Wrote: [ -> ]Thank you. I was thinking more from the calc point of view. If I had another program nib() that inputs a string of 01's and converts it to a string of -101's, how should I do the composite
nib(bin(13)) so that the quotes are not part of the input?
Not part of the input for which program? For nib()? I am also not sure what you mean by a string of "-101's". Right now, your bin() program returns a string. Do you wish the string to actually be a number object? For example:
EXPR("10110") will convert the string "10110" to the decimal number 10110.
Is this what you meant? Perhaps you can share what the goal of nib() is? Is it supposed to count the number of nibbles in the binary representation?
I was thinking of doing compositions of programs. nib() was juts an example, it could be taking a string of 01 and converting that into a string of other characters, for example, replacing 0 by 01 and 1 by 1.
If the input to nib() was the output to bin(), I was not sure how to handle the quotation marks in the output to bin().
But doing as you say, writing RETURN(EXPR(B)) works for me. Thank you.
(12-11-2013 10:02 PM)Alberto Candel Wrote: [ -> ]I was thinking of doing compositions of programs. nib() was juts an example, it could be taking a string of 01 and converting that into a string of other characters, for example, replacing 0 by 01 and 1 by 1.
If the input to nib() was the output to bin(), I was not sure how to handle the quotation marks in the output to bin().
But doing as you say, writing RETURN(EXPR(B)) works for me. Thank you.
It may also be worthwhile to consider leaving the result as a string since the HP Prime has the ability to work with strings (even substitute/replace characters or substrings). Press the [Help] key, then press the [Tree] menu item. Expand the Program Catalogue within the help tree, and do likewise for Commands Menu. You should see a list of string commands.
Hello,
(12-11-2013 07:11 PM)Alberto Candel Wrote: [ -> ]Thank you. I was thinking more from the calc point of view. If I had another program nib() that inputs a string of 01's and converts it to a string of -101's, how should I do the composite
nib(bin(13)) so that the quotes are not part of the input?
In your example: nib(bin(13)) 13 is a number (1+1+1...+1 13 times). Numbers are special type of objects in the system which have limitations (12 digits from 0 to 9 for example) and are interpreted in specific ways...
It might be that in your program, whaever you want to do with your input does fit in these limitations. In this case, you can use a number (a real to be exact) and be happy.
It might be that these limitations do block you. In this case, you will need to either change what you are trying to do, or use some other object type to do it.
for example, to represent binary number, you use 0 and 1... but you most likely want to have number with more than 12 digits! therefore, reals are not adapted.
you could use a list of reals (since lists have arbitrary size), but this has some issues. for a start, a list takes a lot of RAM (around 32 bytes per real)... and might not display in a way that suits you.
Since you are unfortunately not at liberty to change how objects get displayed, it pay to use a good match in your objects....
a String, beeing just a sequence of characters is very versatile, fast to use and takes little memory (2 bytes per characters)... so it can work well...
note that you can access (in read and write) characters of a string stored in a variable by doing var(n) where n is the character index (from 1 to string size). This is the fastest way to modify a string.
Cyrille
Han, Cyrille, thank you, this is very useful.
One more question about strings: can a string be used as input to a program? if so, how? I get an error if I input something like "1011"
-a
(12-13-2013 08:27 AM)Alberto Candel Wrote: [ -> ]Han, Cyrille, thank you, this is very useful.
One more question about strings: can a string be used as input to a program? if so, how? I get an error if I input something like "1011"
-a
It's hard to pinpoint the cause of your error since we don't have the source code. But to answer your question -- yes, a string may be used as the input but it depends on how you input the string. For example:
Code:
EXPORT PSTR(s)
BEGIN
PRINT("You entered: " + s);
END;
If you run this program from the command line with: PSTR("hello world") then the program runs without any problems. However, if you run this using the "Run" menu option from the Program Catalog, then you will see an input screen in which you are asked to input the value of s. The problem is that this method of running the program automatically initializes the local variable s to a real-valued variable with a default value of 0, and expects a real-valued input.