Author Topic: Is there some magic about the number 1?  (Read 2082 times)

0 Members and 1 Guest are viewing this topic.

Offline JohnUKresults

  • Newbie
  • Posts: 40
    • View Profile
Is there some magic about the number 1?
« on: May 14, 2021, 12:31:19 pm »
Hi folks

I have a short piece of code within a large program which is driving me mad!! It works fine if I run it as a stand alone bit of code but not when I have it within the program.

The object of the code is to set up some csv files with fixed data fields, the number and names of the files are "lap" plus the lap number. When I run as the separate little program, it correctly installs lap1, lap2 etc. but when I am running it within the main program, even though the inkeys pause shows the correct file name of lap1 having been strung together, it never opens a lap1 file. Frustratingly, if I set the loop to start at 0 it will open a lap 0, then skip lap1 and go to lap 2 - all subsequent lap files open just fine.

I can't see where it's going wrong. Why should it skip file 1 in the main program?

Ignore all the While inkey$="":wend bits - I just put those in so I could see the variables and file names.

Here are the code snippets - the first is the stand-alone version, modified slightly as it's not using a file name string or looking up in a separate random access file:
Code: QB64: [Select]
  1. REM *** create lap files ***
  2.  
  3. FOR x% = 1 TO 5
  4.     x$ = _TRIM$(STR$(x%))
  5.     PRINT x%, x$: WHILE INKEY$ = "": WEND
  6.     lapfile$ = "c:\race\rrclaps\test2\" + "lap" + x$ + ".csv"
  7.     PRINT lapfile$: WHILE INKEY$ = "": WEND
  8.     filenum% = 10 + x%
  9.     OPEN lapfile$ FOR OUTPUT AS filenum%
  10.     FOR y% = 1 TO 10
  11.         WRITE #filenum%, _TRIM$(STR$(y%)), "00:00:00.000", "00:00:00.000", "00:00:00.000"
  12.     NEXT y%
  13.  
  14. NEXT x%
  15.  
  16.  

This is the same code but within the main program, fil$ being the current file name and laps% being the number of laps to be run. File 5 stores a list of race numbers, test2 is the filename for testing, so it's hard-coded into the short program for clarity, but uses fhe fil$ name to point to the right folder in the main program:
Code: QB64: [Select]
  1.  
  2.             REM *** create lap files ***
  3.             FOR x% = 1 TO laps%
  4.                 x$ = _TRIM$(STR$(x%))
  5.                 PRINT x%, x$: WHILE INKEY$ = "": WEND
  6.                 lapfile$ = "c:\race\rrclaps\" + fil$ + "\" + "lap" + x$ + ".csv"
  7.                 PRINT lapfile$: WHILE INKEY$ = "": WEND
  8.                 filenum% = 10 + x%
  9.                 OPEN lapfile$ FOR OUTPUT AS filenum%
  10.                 FOR y% = 1 TO loaded%
  11.                     GET 5, y%
  12.                     WRITE #filenum%, _TRIM$(num$(1)), "00:00:00.000", "00:00:00.000", "00:00:00.000"
  13.                 NEXT y%
  14.             NEXT x%
  15.  

Anyone got any thoughts?

Offline justsomeguy

  • Newbie
  • Posts: 47
    • View Profile
Re: Is there some magic about the number 1?
« Reply #1 on: May 14, 2021, 12:40:46 pm »
You might try using 'filenum% = FREEFILE' instead of 'filenum% = 10 + x%' on line 8. Just in case you are using that file number somewhere else in your program.

Offline JohnUKresults

  • Newbie
  • Posts: 40
    • View Profile
Re: Is there some magic about the number 1?
« Reply #2 on: May 14, 2021, 12:46:53 pm »
That's a plan!!

I'll try it with filenum%=20+x% first.

Don't want to use FREEFILE as I need to fix in my mind which filenumbers will be used for when I have to be looking through each of the lap files.

It's quite possible that 11 is already being used - hadn't thought of that.

Cheers

Offline JohnUKresults

  • Newbie
  • Posts: 40
    • View Profile
Re: Is there some magic about the number 1?
« Reply #3 on: May 14, 2021, 12:49:23 pm »
Hi. That's nailed it. Knew it would be something stupid!!

Thanks for your pointer. :-)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Is there some magic about the number 1?
« Reply #4 on: May 14, 2021, 01:30:44 pm »
Number 1 is special because it's the beginning of all the other, then it's the multiplicative identity and plenty of other special properties, even in our culture, most prefer to be number 1.

For file numbers, maybe not so much :)
« Last Edit: May 14, 2021, 01:31:51 pm by bplus »

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
Re: Is there some magic about the number 1?
« Reply #5 on: May 14, 2021, 07:00:31 pm »
Number one is easier and faster than number two.

Offline Fifi

  • Forum Regular
  • Posts: 181
    • View Profile
    • My small QB64 contribution
Re: Is there some magic about the number 1?
« Reply #6 on: May 19, 2021, 08:11:57 am »
Hi doppler,

Number one is easier and faster than number two.

In general computing (loop, table, array, etc.) is'nt the Number One element 0? ;)

BTW, did you test the beta of my install script I made for you on your Raspberry Pi?

Looking forward to know if it's working.
It's better to look like an idiot for a short time while asking something obvious to an expert than pretending to be smart all your life. (C) Me.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Is there some magic about the number 1?
« Reply #7 on: May 19, 2021, 11:02:25 am »
Number one is easier and faster than number two.

Fifi has a point when it comes to easier and faster doing number 0 beats them all! :O

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
Re: Is there some magic about the number 1?
« Reply #8 on: May 20, 2021, 09:53:05 am »
@Fifi @bplus Did my rude joke over your heads ?  Think really crude. No math involved.

fifi, will play this week-end. trying it out.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Is there some magic about the number 1?
« Reply #9 on: May 20, 2021, 12:32:06 pm »
@Fifi @bplus Did my rude joke over your heads ?  Think really crude.

Quote
Number one is easier and faster than number two.

This is true, but neither is as fun and enjoyable as a sixty-nine.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Is there some magic about the number 1?
« Reply #10 on: May 21, 2021, 03:42:19 am »
One is the loneliest number that you'll ever do
Two can be as bad as one
It's the loneliest number since the number one, ah.

 - Three Dog Night

p.s. Number 0 requires a cork, maybe two of them.
It works better if you plug it in.