Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assign print using to a string.
#1
An interesting code snippet to get a string variable from print using:

Code: (Select All)
'sample assigning formatted string from print using to a variable:
Rem x$ = Print Using "###"; 100
X = FreeFile
F$ = "tempfile.bak"
Print "Enter value";
Input Z#: If Z# = 0# Then Z# = 100#
Print "Enter format";
Input Z$: If Z$ = "" Then Z$ = "###"
'write print using value
Open F$ For Output As #X
Print #X, Using Z$; Z#
'read print using value
Close
Open F$ For Input As #X
If EOF(X) = 0 Then
  Line Input #X, S$
  Print S$ ' this is the output value
End If
Close
End
Although I cannot find a more efficient way to do this..

Erik.
Reply
#2
(03-30-2024, 03:29 AM)eoredson Wrote: An interesting code snippet to get a string variable from print using:

Code: (Select All)
'sample assigning formatted string from print using to a variable:
Rem x$ = Print Using "###"; 100
X = FreeFile
F$ = "tempfile.bak"
Print "Enter value";
Input Z#: If Z# = 0# Then Z# = 100#
Print "Enter format";
Input Z$: If Z$ = "" Then Z$ = "###"
'write print using value
Open F$ For Output As #X
Print #X, Using Z$; Z#
'read print using value
Close
Open F$ For Input As #X
If EOF(X) = 0 Then
  Line Input #X, S$
  Print S$ ' this is the output value
End If
Close
End
Although I cannot find a more efficient way to do this..

Erik.
Hmmm, that's interesting. In other words what you are looking for would be something like this:

Num = 100
use$ = "This is a test -> ###"
s$ = USING use$, Num

s$ would then contain: This is a test -> 100

I've always used USING to format output to a printer or file but I never gave any thought to USING being utilized in this manner. It's an interesting concept though. The only way I see to do this (besides the method you showed above) would be to create functions and/or subs to preformat strings for you.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#3
(03-30-2024, 04:17 AM)TerryRitchie Wrote:
(03-30-2024, 03:29 AM)eoredson Wrote: An interesting code snippet to get a string variable from print using:

Code: (Select All)
'sample assigning formatted string from print using to a variable:
Rem x$ = Print Using "###"; 100
X = FreeFile
F$ = "tempfile.bak"
Print "Enter value";
Input Z#: If Z# = 0# Then Z# = 100#
Print "Enter format";
Input Z$: If Z$ = "" Then Z$ = "###"
'write print using value
Open F$ For Output As #X
Print #X, Using Z$; Z#
'read print using value
Close
Open F$ For Input As #X
If EOF(X) = 0 Then
  Line Input #X, S$
  Print S$ ' this is the output value
End If
Close
End
Although I cannot find a more efficient way to do this..

Erik.
Hmmm, that's interesting. In other words what you are looking for would be something like this:

Num = 100
use$ = "This is a test -> ###"
s$ = USING use$, Num

s$ would then contain: This is a test -> 100

I've always used USING to format output to a printer or file but I never gave any thought to USING being utilized in this manner. It's an interesting concept though. The only way I see to do this (besides the method you showed above) would be to create functions and/or subs to preformat strings for you.

My old Format$ Function (which I've apparently lost somewhere and had to reshare here: https://qb64phoenix.com/forum/showthread.php?tid=2554 ) does this for us.  Send if a value and a format string, and then get back the result in the format you specified.  Smile

Same basic concept as this as well, except it uses SCREEN instead of file access to achieve the same results.
Reply
#4
You mean something like this:

Code: (Select All)
'another method to assign a formatted value to a string
Color 7
Cls
Print Using "###"; 100
For X = 1 To 30
  Y = Screen(1, X)
  If Y Then s$ = s$ + Chr$(Y)
Next
Cls
Print "The output value is ";
Print RTrim$(s$)
End
Reply
#5
The only 2 problems with using the screen is:

1)Have to store/restore area of screen being written to.
2)What if print using output is greater than 80 chars.

Otherwise, yes, screen is more  efficient then file access..

Erik.
Reply
#6
(04-01-2024, 02:54 AM)eoredson Wrote: The only 2 problems with using the screen is:

1)Have to store/restore area of screen being written to.
2)What if print using output is greater than 80 chars.

Otherwise, yes, screen is more  efficient then file access..

Erik.

Steve has shown how you can do this here:

https://qb64phoenix.com/forum/showthread.php?tid=2554

Just create another SCREEN 0 image of any size you like. It's quite clever.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#7
(04-01-2024, 02:54 AM)eoredson Wrote: The only 2 problems with using the screen is:

1)Have to store/restore area of screen being written to.
2)What if print using output is greater than 80 chars.

Otherwise, yes, screen is more  efficient then file access..

Erik.

1) Just make the screen STATIC so you don't need to create/free it over and over.  It's only going to be a few hundred bytes of memory in the end, after all.

2) You're going to format a single number to more than 80 characters?  That's SOME number!!   Even then, the solution is to just be certain you make the screen large enough to hold the value to begin with.

_NEWIMAGE(800,1,0)  <-- Is 800 characters enough for you to format your number?
_NEWIMAGE(9999, 1, 0)  <--- is 9,999 characters enough?  A normal screen 0 screen is 80x25 characters = 4000 -- you've now got over twice that limit!

Just change the size of that _NEWIMAGE so it's large enough to format that value that you want formatted.  Wink
Reply
#8
Try:

Print Using "&"; String$(800, "#")

is greater than 80 char...

Hee.

https://qb64phoenix.com/qb64wiki/index.php/PRINT_USING

Or something useless like:

Code: (Select All)
For x = 1 To 1024
   x$ = x$ + Str$(x)
Next
Print Using "&"; x$
End
Reply
#9
Try: 

Code: (Select All)
Print FormatStr(String$(800, "#"), "&")

Function FormatStr$ (temp$, using$)
    Static tempimage
    If tempimage = 0 Then tempimage = _NewImage(10000, 1, 0)
    d = _Dest: s = _Source
    _Dest tempimage: _Source tempimage
    Cls
    Print Using using$; temp$;
    For i = 1 To _Width
        p = Screen(1, i)
        If p = 0 Then Exit For
        text$ = text$ + Chr$(p)
    Next
    _Dest d: _Source s
    FormatStr$ = RTrim$(text$)
End Function
As I mentioned, just change the size of the NewImage so that it's large enough to hold the number of characters needed.  If you need more than 32,000 characters (which is a little less than the actual limit, I think), you're doing something wrong.  Break that damn line down into segments and process it one chunk at a time!


PS: I want to see the screen where you're printing your output to.  What size monitor are you using for: 
For x = 1 To 1024
x$ = x$ + Str$(x)
Next
Print Using "&"; x$

At a max of characters per value (space + 4 digit number + space), that's about 6000 characters. Since Print UUsing doesn't do word wrap, you'd almost expect to see all 6000 of those characters on a single line. At 8 pixels width, that's only 48000 pixels -- while Windows itself only allows *any* screen to be 32000 pixels...

Something seems wrong here, mate. Tongue
Reply
#10
Quote:Something seems wrong here, mate.
I was just kidding. I have no use for such code.
Reply




Users browsing this thread: 1 Guest(s)