QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: johnno56 on June 30, 2021, 04:26:52 pm

Title: Print Using
Post by: johnno56 on June 30, 2021, 04:26:52 pm
Does anyone know how to simulate the Print Using command? I would like to know how the command formats the output based on the 'pattern' given by the command. Just curious.. If this is too trivial, just let me know. I will not be offended. But, remember, your avatar and your embedded GPS information will be added to my black list... Moo Ha Ha....
Title: Re: Print Using
Post by: SMcNeill on June 30, 2021, 05:47:57 pm
Moo Ha Ha....

Is that your impression of a cow laughing?  MuHaha?!!

Somewhere here on the forums, I have a FUNCTION Format$ which can be used to simulate PRINT USING results.  The solution I used is both rather simplistic and genius -- I just make a temporary text screen page, use PRINT USING to print to it, and then read the characters back from that page before freeing it, and I store the results in a string to return back from the function.  Guaranteed to behave EXACTLY like PRINT USING does with formatting because it simply *IS* PRINT USING in action -- though with the results returned to you via a string and not printed direct to the screen.
Title: Re: Print Using
Post by: bplus on June 30, 2021, 07:08:42 pm
And RhoSigma did an nice all purpose Format$ here:
https://www.qb64.org/forum/index.php?topic=2946.0
Title: Re: Print Using
Post by: johnno56 on June 30, 2021, 07:31:59 pm
SMcNeill:

A laughing cow... That could be used as a brand name for spreadable cheese... lol  Nah. Closest I could get to phonetically copying the laugh of Austin Powers... Come to think of it, English per se, is not a phonetic language... But you must catch my meaning... after all... you are a genius... wink, wink.... thank you for replying so quickly... We, on the other side of the planet, do not expect such rapid responses.... Appreciated.
ps: A soon as I post I will look for your formatter....

bplus: Thank you for the reference. Appreciated.

J
Title: Re: Print Using
Post by: SpriggsySpriggs on June 30, 2021, 07:35:53 pm
I've used some Win32 functions and even just printf as a function in a declare block that do really the same thing as PRINT USING.
Title: Re: Print Using
Post by: George McGinn on June 30, 2021, 10:50:55 pm
I never got around to figuring this out, but sometimes in a program I want to assign a string variable to a formatted value. For example, if I have a number that has 4 or more decimal places, but I only want three for my processing, I can use RhoSigma's routine (or the one in your post) to do that.

As an example:

Code: [Select]
A$ = IndexFormat$("{#.###}", "1.112233", "|")
PRINT "A$ = "; A$



And RhoSigma did an nice all purpose Format$ here:
https://www.qb64.org/forum/index.php?topic=2946.0
Title: Re: Print Using
Post by: SpriggsySpriggs on June 30, 2021, 10:57:33 pm
@George McGinn He's basically doing this: https://docs.microsoft.com/en-us/windows/win32/api/strsafe/nf-strsafe-stringcbprintfa (https://docs.microsoft.com/en-us/windows/win32/api/strsafe/nf-strsafe-stringcbprintfa)
Title: Re: Print Using
Post by: George McGinn on June 30, 2021, 11:10:49 pm
I will have to try it (I found it in the QB64 Development internal c directory)

 Is this new? (I did not find it in the v1.5 stable release of QB64)?

@George McGinn He's basically doing this: https://docs.microsoft.com/en-us/windows/win32/api/strsafe/nf-strsafe-stringcbprintfa (https://docs.microsoft.com/en-us/windows/win32/api/strsafe/nf-strsafe-stringcbprintfa)
Title: Re: Print Using
Post by: RhoSigma on July 01, 2021, 02:20:24 am
I was going to mention my IndexFormat$() function too, when johnno56 came up with this, but did not, because it didn't answer his request.

Does anyone know how to simulate the Print Using command? I would like to know how the command formats the output based on the 'pattern' given by the command.

And BTW, nobody did answer it here, we are all much to fast here giving alternatives or promoting our own made functions. Or as @NOVARSEG mentioned in another post: "99% of all tech questions remain unsolved."

That's why I stepped back since the beginning of the year, making much fewer posts here than the years before.

EDIT:
Oh, and just to make it sure, this is not about offending anybody here for giving unprecise answers, it's more a resignation on my side.
Title: Re: Print Using
Post by: SMcNeill on July 01, 2021, 09:54:34 am
SMcNeill:

A laughing cow... That could be used as a brand name for spreadable cheese... lol  Nah. Closest I could get to phonetically copying the laugh of Austin Powers... Come to think of it, English per se, is not a phonetic language... But you must catch my meaning... after all... you are a genius... wink, wink.... thank you for replying so quickly... We, on the other side of the planet, do not expect such rapid responses.... Appreciated.
ps: A soon as I post I will look for your formatter....

bplus: Thank you for the reference. Appreciated.

J

Code: QB64: [Select]
  1. PRINT format$("###.###", "123.456789")
  2. PRINT format$("###,.##", "123456789.987654321")
  3.  
  4.  
  5. FUNCTION format$ (template AS STRING, text AS STRING)
  6.     d = _DEST: s = _SOURCE
  7.     n = _NEWIMAGE(80, 80, 0)
  8.     _DEST n: _SOURCE n
  9.     PRINT USING template; VAL(text)
  10.     FOR i = 1 TO 79
  11.         t$ = t$ + CHR$(SCREEN(1, i))
  12.     NEXT
  13.     IF LEFT$(t$,1) = “%” THEN t$ = MID$(t$,2)
  14.     format$ = t$
  15.     _DEST d: _SOURCE s
  16.     _FREEIMAGE n
Title: Re: Print Using
Post by: johnno56 on July 01, 2021, 04:38:59 pm
Cool. Nicely done.

Many thanks.

J
Title: Re: Print Using
Post by: George McGinn on July 01, 2021, 07:48:09 pm
@SMcNeill - Which PRINT statement is the one that works? The PRINT in the function or the PRINT calling it?? (rhetorical)

It may be simple, but it just doesn't work, not the same as the @RhoSigma routine.

Not only does the PRINT statement I use it in not work, I cannot assign a variable (see image below) to the result of the format function.

If I were doing a simple PRINT USING "{MASK}" with no string values (such as PRINT USING "The interest charged is #.### percent") -- see second attached image.

It is a good start, but needs more work.

  [ This attachment cannot be displayed inline in 'Print Page' view ]  

  [ This attachment cannot be displayed inline in 'Print Page' view ]  


Code: QB64: [Select]
  1. PRINT format$("###.###", "123.456789")
  2. PRINT format$("###,.##", "123456789.987654321")
  3.  
  4.  
  5. FUNCTION format$ (template AS STRING, text AS STRING)
  6.     d = _DEST: s = _SOURCE
  7.     n = _NEWIMAGE(80, 80, 0)
  8.     _DEST n: _SOURCE n
  9.     PRINT USING template; VAL(text)
  10.     FOR i = 1 TO 79
  11.         t$ = t$ + CHR$(SCREEN(1, i))
  12.     NEXT
  13.     IF LEFT$(t$,1) = “%” THEN t$ = MID$(t$,2)
  14.     format$ = t$
  15.     _DEST d: _SOURCE s
  16.     _FREEIMAGE n
Title: Re: Print Using
Post by: SMcNeill on July 01, 2021, 09:19:13 pm
You're seeing the formatting on the next line by there not being a _TRIM$ in there.

Code: QB64: [Select]
  1. Print "FOO: "; format$("###.###", "123.456789")
  2. Print format$("###,.##", "123456789.987654321")
  3.  
  4.  
  5. Function format$ (template As String, text As String)
  6.     d = _Dest: s = _Source
  7.     n = _NewImage(80, 80, 0)
  8.     _Dest n: _Source n
  9.     Print Using template; Val(text)
  10.     For i = 1 To 79
  11.         t$ = t$ + Chr$(Screen(1, i))
  12.     Next
  13.     If Left$(t$, 1) = "%" Then t$ = Mid$(t$, 2)
  14.     format$ = _Trim$(t$)
  15.     _Dest d: _Source s
  16.     _FreeImage n
  17.  

The above should fix that.
Title: Re: Print Using
Post by: George McGinn on July 01, 2021, 09:35:08 pm
@SMcNeill - AWESOME!

Thanks. That does the trick. I would have taken a crack at a fix, but I'm buried deep in learning QT5 on Linux in C++!

George.

You're seeing the formatting on the next line by there not being a _TRIM$ in there.

Code: QB64: [Select]
  1. Print "FOO: "; format$("###.###", "123.456789")
  2. Print format$("###,.##", "123456789.987654321")
  3.  
  4.  
  5. Function format$ (template As String, text As String)
  6.     d = _Dest: s = _Source
  7.     n = _NewImage(80, 80, 0)
  8.     _Dest n: _Source n
  9.     Print Using template; Val(text)
  10.     For i = 1 To 79
  11.         t$ = t$ + Chr$(Screen(1, i))
  12.     Next
  13.     If Left$(t$, 1) = "%" Then t$ = Mid$(t$, 2)
  14.     format$ = _Trim$(t$)
  15.     _Dest d: _Source s
  16.     _FreeImage n
  17.  

The above should fix that.
Title: Re: Print Using
Post by: Aurel on July 04, 2021, 06:29:01 pm
Quote
I've used some Win32 functions

well sprigssy tell us more?
do you maybe think about this one :

HRESULT StringCchPrintfW(LPWSTR pszDest, size_t cchDest, LPCWSTR pszFormat, ...);
Title: Re: Print Using
Post by: SpriggsySpriggs on July 04, 2021, 06:30:26 pm
Yes, I've used StringCchPrintf. Also StringCbPrintf