Page 1 of 1

A set number with leading zeros, can't seem to figure it out

PostPosted: Sat Jun 24, 2017 8:44 pm
by PGamble777
Hi all, I have a problem I've been working on but I've developed brain fog and can't for the life of me figure out how
to achieve the results I need, it seems to me it's probably an easy thing to achieve.. :/

I'm using Neobook 5.0.

I have four text fields:
a [URLtoProcess] field (example content: "http://www.here.com/00001.ext"),
a [FromNumber] field (example content: "1"),
a [ToNumber] field (example content: "11100"),
and a [DigitstoAdd] field (example content: "5").

I need to write a function which can write line after line of the [URLtoProcess] upto [ToNumber] in a ListBox, but with the number at the near end replaced and the length of that number dependent on [DigitstoAdd].
I will have the user type and replace the number in the URL with the characters "%#%".

I have a very basic script in a button which populates a ListBox, so far:

Code: Select all
    Loop "[FromNumber]" "[ToNumber]" "[UptoHere]"
    StrReplace "[URLtoProcess]" "%#%" "[UptoHere]" "[FileIteration]"
    ListBoxAddItem "ListBox1" "0" "[FileIteration]"
    EndLoop


But I cannot figure out how to set the number length to be based on [ToNumber], eg; keep it to only those number of characters for the number in total and not to simply add the total [DigitstoAdd] on the front of each number. (I mean like; If the [ToNumber] is say "11434" and the [DigitstoAdd] is set to '5', for the output at say iteration '234' to be "00234" and not "00000234", I hope that makes sense?)

Any help would be very greatly appreciated.

Re: A set number with leading zeros, can't seem to figure it

PostPosted: Sat Jun 24, 2017 10:13 pm
by Gaev
PGamble777:

Try something like this ...
Code: Select all
SetVar "[MaxZeroes]" "!00000000000000000"
Loop "[FromNumber]" "[ToNumber]" "[UptoHere]"

    StrLen "[UptoHere]" "[UptoHereLen]
    Math "[DigitsToAdd]-[UptoHereLen]" "0" "[PrefixStringLen]"

    If "[PrefixStringLen] "=" "0"
        SetVar "[PrefixString]" ""
    Else
        SubStr "[MaxZeroes]" "1" "[PrefixStringLen]" "[PrefixString]"
    EndIf

    StrReplace "[URLtoProcess]" "!%#%" "![PrefixString][UptoHere]" "[FileIteration]"
    ListBoxAddItem "ListBox1" "0" "[FileIteration]"
EndLoop


1) Take note of ! marks in commands

2) Might be prudent to use a replace string other than one with % and # sign characters

3) It is late in the night and I have not had a chance to test this code ... so it may have typo errors (hope you are well versed in debugging stuff)

Re: A set number with leading zeros, can't seem to figure it

PostPosted: Sat Jun 24, 2017 11:59 pm
by PGamble777
Thankyou Gaev, that worked well, I had to add a couple of quotation marks which were missing but it works very well! :)

Earlier today I figured another way to do it (with lots and lots of code / very uneconomical), but I've just done a time trial of populating a listbox with both your code and my clunky code and the one I've written seems to populate the listbox quite a bit faster! Which I don't know how... :/

This is the script I'm using currently:

Code: Select all
Loop "[FromNumber]" "[ToNumber]" "[UptoHere]"

If "[DigitstoAdd]" "=" "1"
GoSub "1digit"
EndIf
If "[DigitstoAdd]" "=" "2"
GoSub "2digit"
EndIf
If "[DigitstoAdd]" "=" "3"
GoSub "3digit"
EndIf
If "[DigitstoAdd]" "=" "4"
GoSub "4digit"
EndIf
If "[DigitstoAdd]" "=" "5"
GoSub "5digit"
EndIf

StrReplace "[URLtoProcess]" "%#%" "[ZeroedNumber]" "[FileIteration]"
SetVar "[StatusField]" "Adding - [FileIteration] to Download List"
ListBoxAddItem "DownloadBox" "0" "[FileIteration]"
EndLoop
ListBoxGetItem "DownloadBox" "All" "[DlList]"
SetVar "[StatusField]" "Adding Done - Added a Total of [ToNumber] URLs to the Download List"


And the subroutines are:

Code: Select all
:1digit
SetVar "[ZeroedNumber]" "[UptoHere]"
Return

:2digit
If "[UptoHere]" ">" "9"
SetVar "[ZeroedNumber]" "[UptoHere]"
Return
EndIf
If "[UptoHere]" "<" "10"
SetVar "[ZeroedNumber]" "!0[UptoHere]"
Return
EndIf
Return

:3digit
If "[UptoHere]" ">" "99"
SetVar "[ZeroedNumber]" "[UptoHere]"
Return
EndIf
IfEx "[UptoHere] > 9 AND [UptoHere] < 100"
SetVar "[ZeroedNumber]" "!0[UptoHere]"
Return
EndIf
If "[UptoHere]" "<" "10"
SetVar "[ZeroedNumber]" "!00[UptoHere]"
Return
EndIf
Return

:4digit
If "[UptoHere]" ">" "999"
SetVar "[ZeroedNumber]" "[UptoHere]"
Return
EndIf
IfEx "[UptoHere] > 99 AND [UptoHere] < 1000"
SetVar "[ZeroedNumber]" "!0[UptoHere]"
Return
EndIf
IfEx "[UptoHere] > 9 AND [UptoHere] < 100"
SetVar "[ZeroedNumber]" "!00[UptoHere]"
Return
EndIf
If "[UptoHere]" "<" "10"
SetVar "[ZeroedNumber]" "!000[UptoHere]"
Return
EndIf
Return

:5digit
If "[UptoHere]" ">" "10000"
SetVar "[ZeroedNumber]" "[UptoHere]"
Return
EndIf
IfEx "[UptoHere] > 999 AND [UptoHere] < 10000"
SetVar "[ZeroedNumber]" "0[UptoHere]"
Return
EndIf
IfEx "[UptoHere] > 99 AND [UptoHere] < 1000"
SetVar "[ZeroedNumber]" "!00[UptoHere]"
Return
EndIf
IfEx "[UptoHere] > 9 AND [UptoHere] < 100"
SetVar "[ZeroedNumber]" "!000[UptoHere]"
Return
EndIf
If "[UptoHere]" "<" "10"
SetVar "[ZeroedNumber]" "!0000[UptoHere]"
Return
EndIf
Return


As I said, very CLUNKY code, and I'm limiting myself to 5 digits.. But it populates the listbox faster! I ran a time trial based on 8000 items in 4 digits. I love the simplicity of your code better, not sure how mine is being quicker..

Re: A set number with leading zeros, can't seem to figure it

PostPosted: Sun Jun 25, 2017 6:04 am
by Gaev
PGamble777:

You did not provide numbers to show what kind of time difference you experienced (nor the power of your PC).

I don't know why one piece of code takes longer to execute than another. Some NeoBook commands take longer to be serviced than others; inside a large Loop/EndLoop, these differences are magnified.

Also, there might be subtle differences in the way you defined your ListBoxes for the two runs. And I hope the ListBoxes were cleared of all existing entries before each test run (adding 8,000 entries to a list that already has 8,000 entries takes much longer.

Talking of ListBoxes, I see in your latest code that you are only using them to collect all the URLs. If speed of execution is important, instead of ...
Code: Select all
ListBoxAddItem "DownloadBox" "0" "[FileIteration]"

... and (at end) ...

ListBoxGetItem "DownloadBox" "All" "[DlList]"

... you might try ...
Code: Select all
SetVar "[DList]" "![Dlist][FileIteration][#13][#10]"

... of course, make sure you clear [DList] each time before your Loop/EndLoop.

Re: A set number with leading zeros, can't seem to figure it

PostPosted: Sun Jun 25, 2017 7:58 am
by virger
Y si intenta esto..
And if you try this...

Code: Select all
setvar "[FromNumber]" "1"
setvar "[ToNumber]" "11100"
setvar "[DigitstoAdd]" "5"

../////////////////
setvar "[URLtoProcess]" "http://www.here.com/"
SetVar "[Ceros]" "0000000000"
substr "[Ceros]" "1" "[DigitstoAdd]-1" "[Ceros]"

Loop "[FromNumber]" "[ToNumber]" "[UptoHere]"
     StrLen "[UptoHere]" "[UptoHereLen]"
     SubStr "[Ceros][UptoHere]" "[UptoHereLen]" "99" "[dt]"
     ... TESTING
     ListBoxAddItem "DownloadBox" "0" "[URLtoProcess][Dt].ext    ---- [UptoHere]"
EndLoop


Gracias por leerme
Thanks for reading me