topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Wednesday December 11, 2024, 8:39 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: DONE: Generate sequential serial numbers  (Read 36464 times)

pagebooks

  • Participant
  • Joined in 2012
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
DONE: Generate sequential serial numbers
« on: June 11, 2012, 07:14 AM »
I need a script which on invocation will write a six digit serial number which is the preceding invocation's value incremented by a fixed integer. Presumably the initial (starting serial number) and the increment could be set by editing the script. It is not necessary that the most recent serial number be remembered between machine shut downs.

If someone could provide me with such a script I'd be exceedingly appreciative.

Josh

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #1 on: June 11, 2012, 10:04 PM »
Hello, pagebooks, and welcome to DonationCoder.  Now, when you say "write a six digit serial number," do you mean you want to 1) press a hotkey and then 2) have the script send the keystrokes of the generated serial number wherever the text caret is?


pagebooks

  • Participant
  • Joined in 2012
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #2 on: June 12, 2012, 07:00 AM »
Yes that's exactly what I want. . . and when I press that hotkey again it will send the keystrokes of the previous number incremented by a fixed integer.

Many thanks for your reply,

Josh

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #3 on: June 12, 2012, 07:06 AM »
Are you familiar with AutoHotkey scripting?  If so, here's a simple script that does what you want.

Code: Autohotkey [Select]
  1. mySerial := 1
  2. myFactor := 2
  3.  
  4. ^g:: ; Ctrl+G, change to whatever you want.
  5. {
  6.     SendInput, % mySerial
  7.     mySerial += myFactor
  8. }
  9. Return

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,939
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #4 on: June 12, 2012, 07:21 AM »
@skwire:
You just made his homework... :P

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #5 on: June 12, 2012, 07:40 AM »
Yeah, I know.   :-[

pagebooks

  • Participant
  • Joined in 2012
  • *
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #6 on: June 12, 2012, 10:26 AM »
At age 79 I admit that I struggle with contemporary technology, but I have skwire's script up and running and I'm very pleased with the results. Thanks, I'll make a donation.

Josh

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #7 on: June 12, 2012, 10:43 AM »
Great to hear.   :up:   I'm glad we were able to help you out.  =]

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #8 on: June 12, 2012, 02:51 PM »
I still don't quite get what SubStr() is doing, but from a forum snippet I added the "6 digit" part


mySerial := 1
myFactor := 2

^g:: ; Ctrl+G, change to whatever you want.
{
mySerial := SubStr("000000" . mySerial, -5)
    SendInput, % mySerial
    mySerial += myFactor
}
Return


Ruffnekk

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 332
  • Uhm yeah...
    • View Profile
    • RuffNekk's Crypto Pages
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #9 on: June 21, 2012, 05:18 AM »
I still don't quite get what SubStr() is doing, but from a forum snippet I added the "6 digit" part


mySerial := 1
myFactor := 2

^g:: ; Ctrl+G, change to whatever you want.
{
mySerial := SubStr("000000" . mySerial, -5)
    SendInput, % mySerial
    mySerial += myFactor
}
Return



I don´t know AHK very well, but I can imagine SubStr() is Substitute String. Which brings me to the next point, that you are effectively converting your integer to a string. The proper way do it is converting the return value and leaving the integer alone:


mySerial := 1
myFactor := 2

^g:: ; Ctrl+G, change to whatever you want.
}
    SendInput, % SubStr("000000" . mySerial, -5)
    mySerial += myFactor
}
Return


I don´t know if AHK cares about it, but it´s good practice in programming and other languages would not accept such an implicit conversion. ;)
Regards,
RuffNekk

Programming is an art form that fights back.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,914
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #10 on: June 21, 2012, 05:44 AM »
I dont know AHK but i can see clearly what it's doing:

SubStr is common name for a function that extracts part of a string (characters at start, end, or middle).

-5 is used by some SubStr implementation to mean the last 5 characters.

So it looks like this is creating a string that starts with "000000" followed by the serial number string (like "12"), to yield something like "00000012",
and then grabbing the string made up of the last 5 characters, e.g. "00012".

In other words it's generating a 6 digit serial number string, padded on the left with 0s.

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #11 on: June 21, 2012, 02:10 PM »
I don´t know if AHK cares about it, but it´s good practice in programming and other languages would not accept such an implicit conversion. ;)

The "I don't know" part above is the salient phrase. Goolge COM and Variant. As for the bit about SubStr(), such sarcasm is weak.  That's all you can come up with?  Read the help for SubStr() and analyze it on here. Then maybe I'll ask when something befuddles me.

If you can analyze this run-on paragraph, I'd appreciate it

================================================================

SubStr(String, StartingPos [, Length]) [v1.0.46+]: Copies a substring from String starting at StartingPos and proceeding rightward to include at most Length characters (if Length is omitted, it defaults to "all characters"). For StartingPos, specify 1 to start at the first character, 2 to start at the second, and so on (if StartingPos is beyond String's length, an empty string is returned). If StartingPos is less than 1, it is considered to be an offset from the end of the string. For example, 0 extracts the last character and -1 extracts the two last characters (but if StartingPos tries to go beyond the left end of the string, the extraction starts at the first character). Length is the maximum number of characters to retrieve (fewer than the maximum are retrieved whenever the remaining part of the string is too short). Specify a negative Length to omit that many characters from the end of the returned string (an empty string is returned if all or too many characters are omitted). Related items: RegExMatch(), StringMid, StringLeft/Right, StringTrimLeft/Right.

================================================================


« Last Edit: June 21, 2012, 02:36 PM by MilesAhead »

Ruffnekk

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 332
  • Uhm yeah...
    • View Profile
    • RuffNekk's Crypto Pages
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #12 on: June 22, 2012, 01:31 AM »
No sarcasm intented. Just saying that when you use...

Code: Autohotkey [Select]
  1. mySerial := SubStr("000000" . mySerial, -5)

...you are trying to put a string into an integer variable. Even when using
var
in languages like C#, the moment you declared
mySerial := 1
it is an integer.

Even when AHK would allow this and store the string in
mySerial
, then the statement
mySerial += myFactor
would fail or give unexpected results.
Regards,
RuffNekk

Programming is an art form that fights back.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #13 on: June 22, 2012, 02:20 AM »
... in languages like C#, ...

But it's not C# - each language has it's own set of rules and restrictions.

AutoHK allows typeless variables, as does AutoIt and REXX.

Whether or not a variable is reused for a type other than what it started with comes down to readability and if it works.

Why create more variables than you need?

Ruffnekk

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 332
  • Uhm yeah...
    • View Profile
    • RuffNekk's Crypto Pages
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #14 on: June 22, 2012, 02:30 AM »
Like I said, I was just trying to give a tip on good practice in coding.

Please explain to me how the code would work correctly, even if variable data types are allowed. What is the outcome of mySerial += myFactor when mySerial is something like "000001" (a string). I would be very surprised if you claimed it would equal 3 (integer).

Anyway, I wasn´t trying to step on any one´s toes here. Keep up the bad practice if you like!  :Thmbsup:
Regards,
RuffNekk

Programming is an art form that fights back.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #15 on: June 22, 2012, 03:00 AM »
Please explain to me how the code would work correctly, even if variable data types are allowed. What is the outcome of mySerial += myFactor when mySerial is something like "000001" (a string). I would be very surprised if you claimed it would equal 3 (integer).

While I'm no good at AutoHK, it did spring from AutoIt - so here's a little excerpt from the help file:
In AutoIt there is only one datatype called a Variant.  A variant can contain numeric or string data and decides how to use the data depending on the situation it is being used in.  For example, if you try and multiply two variants they will be treated as numbers, if you try and concatenate (join) two variants they will be treated as strings.

If a string is used as a number, an implicit call to Number() function is done. So if it doesn't contain a valid number, it will be assumed to equal 0.

So this:
Code: AutoIt [Select]
  1. $mySerial = '000001gh'
  2. $myFactor = 2
  3. $mySerial += $myFactor  ; With the implicit call becomes: Number($mySerial) += $myFactor

$mySerial = 3

Add this:
Code: AutoIt [Select]
  1. StringFormat('%06s', $mySerial) ; With the implicit call becomes: StringFormat('%06s', String($mySerial))

$mySerial = '000003'

Keep up the bad practice if you like!  :Thmbsup:

I'll take function over form any day as long as it gets the job done  ;)
« Last Edit: June 22, 2012, 03:20 AM by 4wd »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #16 on: June 22, 2012, 06:20 PM »
Like I said, I was just trying to give a tip on good practice in coding.

Please explain to me how the code would work correctly,
...


I think you are taking the wrong approach. Since you are expert on correct programming practice it should be trivial to post original working code in ahk that does what mine does, but in a superior fashion. If it's better I'll say so and probably copy it. But until I see working code from you in ahk, pardon me, but I'll ignore your comments on this issue.  Anyone can heckle. Producing something that works is more impressive. :)


Ruffnekk

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 332
  • Uhm yeah...
    • View Profile
    • RuffNekk's Crypto Pages
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #17 on: June 25, 2012, 01:16 AM »
I´m not taking the wrong approach, I was explaining something that is useful for general practice in programming. Right off the bat I admitted I don´t know AHK and I wasn´t specifically criticizing AHK. Anyway, I don´t want or have to prove anything to anyone; I´ve been a senior programmer for over 15 years at the same multinational company and if you refuse to take good advice for what it is, then that´s your loss. No hard feelings, man. You´re taking this way too seriously.  :-*
Regards,
RuffNekk

Programming is an art form that fights back.

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #18 on: June 25, 2012, 03:54 AM »
...I don´t want or have to prove anything to anyone; I´ve been a senior programmer for over 15 years at the same multinational company and if you refuse to take good advice for what it is, then that´s your loss.
Sorry to chip in, but really, this seems to be becoming an absurd discussion.
In what the speaker says here there seem to be not a few direct/implied logical fallacies:
  • there is an implied argumentum ad hominem (argument against the person, including, for example @MilesAhead) - viz: "he's inexperienced/unqualified".
  • there is an argumentum ad ignorantiam (forwarding a proposition without any certain proof) - that the speaker is implicitly "an authority".
  • there is an unproven (QED) and implied argumentum ad verecundiam (appeal to authority) - based on the above fallacy, the implication that because the speaker is an authority he/she is therefore correct in all his/her statements.
  • there is an ignoratio elenchi (a "red herring" or genetic fallacy) - the speaker's opinions of their own qualities or those of @MilesAhead or anyone else are irrelevant as they prove nothing - except perhaps the speaker's own self-opinionated arrogance, in this case at any rate.
  • there is a non sequitur ("it does not follow") - for all we know, the speaker's work experience might have meant 15 long and dreary years with the same company, with annually poor training and repetitive and bad programming practice, with the adverse effects of same being spread multinationally, leaving him angrily stuck in the rut where he is, unacceptable for employment with another company.

I would suggest that if I have the above more or less right, then it could be worth reflecting as to whether there is generally a requirement that programmers need to be able to reason logically to be able to do their job. If there is, and if a person cannot, then a vocational guidance counsellor would probably suggest that programming was an inadvisable vocation for such an individual.
I say this because, without such advice, a person might spend the best part of their working life doing something for which they were naturally ill-suited, and living in a sense of unhappiness, inner frustration, incompleteness and anger as a result. This might be coped with in a sub-optimal manner by, (say) angrily hitting out at or running down others as though they were the cause of the inner turmoil and ego-conflict.

In any event, I would suggest that the speaker's apparently absurd reasoning and rudeness on display in this thread is uncalled for, unnecessary and unconstructive.

Ruffnekk

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 332
  • Uhm yeah...
    • View Profile
    • RuffNekk's Crypto Pages
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #19 on: June 25, 2012, 04:20 AM »
This is my last reply to this thread, because it is utterly absurd the reactions I get. I always thought DonationCoder was a friendly forum, where people are able to discuss something rationally, but I guess I was wrong. I just want to point out once more, that I was only giving advice, I wasn´t claiming anything from authority, nor was I trying to be rude and I most certainly don´t think anything I said was uncalled for or directly aimed at a person or his/her skills.

@IainB: Throwing around some fancy words and Latin phrases makes you neither an authority, nor an intelligent person. On the contrary, your complete post is exactly all the things you accuse someone else of in it. Wanna-be-psychologist, much?
Regards,
RuffNekk

Programming is an art form that fights back.

anandcoral

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 783
    • View Profile
    • Free Portable Apps
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #20 on: June 25, 2012, 07:07 AM »
Good going. Must be global warming effect  :)

Need to change the thread to something 'Generating sequential point counter-point'.

Though I have similar years, company etc., I still find myself learning. I learnt from the code that AHK auto converts to string if string is added to number. Since I use a lot AHK, AutoIt etc. code, this tip will reduce my code.

Regards,

Anand



IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #21 on: June 25, 2012, 08:42 AM »
...I learnt from the code that AHK auto converts to string if string is added to number. Since I use a lot AHK, AutoIt etc. code, this tip will reduce my code...
Oh, I missed that. That's useful. Thanks for pointing that out.    :up:
I am still slowly developing my understanding of AHK-L and that's why I like to read DC-created scripts and discussions about AHK code, though I am not necessarily able to contribute to or fully understand them...   :-[
I also subscribe to the discussions in the forum at http://www.autohotkey.com/community/ in my Google Reader, but it's a bit of a mixed bag, and they are too many and too active for me to be able to keep track of and study them all. I'm sure I miss a lot of useful info.



_____________________________________
"Semper in excremento, solo profundo variat"

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #22 on: June 25, 2012, 04:45 PM »
I think the main risk I've seen using variants and implicit conversion is in comparisons. When I was new to AutoIt3 I got some strange results in comparisons because I didn't realize that reading a value from an .ini file produced a string, no matter the value. Ini files often use 0 for False and 1 for True. It was easy when typing in code to make the error demonstrated by the top half of the code below(esp. since in .ini files strings are not quoted. Easy to see it as boolean instead of string.) That's why I came up with _IniBool() function. It seems trivial but with it you can actually use Boolean values in AutoIt variables and True or False will be written to the .ini using IniWrite. If the user changes it to 1 for true and 0 for false, it still functions as expected.

$val = "1"
If $val Then
MsgBox(0x1040,"test","$val is True" & " " & $val)
Else
MsgBox(0x1040,"test","$val is False" & " " & $val)
EndIf


$val = "0"
If $val Then
MsgBox(0x1040,"test","$val is True" & " " & $val)
Else
MsgBox(0x1040,"test","$val is False" & " " & $val)
EndIf

$val = "1"
If _IniBool($val) Then
MsgBox(0x1040,"test","$val is True" & " " & $val)
Else
MsgBox(0x1040,"test","$val is False" & " " & $val)
EndIf


$val = "0"
If _IniBool($val) Then
MsgBox(0x1040,"test","$val is True" & " " & $val)
Else
MsgBox(0x1040,"test","$val is False" & " " & $val)
EndIf

Func _IniBool($val)
    If $val = "True" Or $val = "1" Then Return True
    Return False
EndFunc   ;==>_IniBool



For the AHK_L code in question this demonstrates that one can stick the result of SubStr() in another dedicated variable. But since there's no comparison it's just the creation of another variable for no real reason. I found out from Lexicos the author of AHK_L when you create a variable, even a local in a function, the storage is there for the life of the program run. He stressed the main danger of doing things like myObject := 0 to clear an object is if you have other references to the same object. In that case you can get side effects/unexpected behavior.  For that reason I modified a small utility I wrote to create an object as a local inside a function, then return the result out to the global. Lexicos says that even though the local variables don't "disappear" when the function returns like compiled program stack variables, the storage is reused on the next function call.  So basically, if I make sure I only see one reference to an object, I can safely clear its contents with myObject := 0. In which case the variable myObject is now a numeric.  But then you can just assign the object returned from the function and its type is restored to Object.

Fun stuff. Here's a link to the discussion where I asked about just setting an object to 0 to clear the contents. The link is pretty much the climax of the topic.

http://www.autohotke...amp;start=15#p544146

Here's ahk sticking the result of SubStr() in a different variable


mySerial := 1
myFactor := 2
myTemp := 0

^g:: ; Ctrl+G, change to whatever you want.
{
;mySerial := SubStr("000000" . mySerial, -5)
  myTemp := SubStr("000000" . mySerial, -5)
  SendInput, % myTemp
  mySerial += myFactor
}
Return

It works. But in this case since I never compare myTemp or mySerial to anything, there's no purpose served.

I also do C#. It's a strongly typed scripting language as opposed to scripting such as AutoIt3 and AHK where they just use Variants for everything.  One benefit of the "one size fits all" Variant is that it makes it easy to interface to system scripting stuff that's all IDispatch based.

In any case, I never held a job with "developer" or "programmer" as the job title. But I've been programming in various languages steadily since I first got hooked in 1986. I guess it just seemed pointless to me and a bit insulting to be offered tips on correct programming practice by someone who doesn't even understand the language in question. It would be like me lecturing someone by saying "I don't know a damn thing about Lisp but if I was doing it in Pascal I sure wouldn't do what you're doing there buddy!"  heh heh

Generally I find it better to ask for working code than argue. If someone can demonstrate what they claim, and I can run i and see their point, the argument is over pretty quickly. Otherwise it tends to degenerate into who claims to have the highest IQ. :)

« Last Edit: June 25, 2012, 05:00 PM by MilesAhead »

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #23 on: June 25, 2012, 06:59 PM »
@MilesAhead: Thanks for the explanation, especially (my emphasis):
...
...For the AHK_L code in question this demonstrates that one can stick the result of SubStr() in another dedicated variable. But since there's no comparison it's just the creation of another variable for no real reason. I found out from Lexicos the author of AHK_L when you create a variable, even a local in a function, the storage is there for the life of the program run. He stressed the main danger of doing things like myObject := 0 to clear an object is if you have other references to the same object. In that case you can get side effects/unexpected behavior.  For that reason I modified a small utility I wrote to create an object as a local inside a function, then return the result out to the global. Lexicos says that even though the local variables don't "disappear" when the function returns like compiled program stack variables, the storage is reused on the next function call.  So basically, if I make sure I only see one reference to an object, I can safely clear its contents with myObject := 0. In which case the variable myObject is now a numeric.  But then you can just assign the object returned from the function and its type is restored to Object....
...
That seems to me to be quite a tidy way of ensuring that you avoid the risk of odd results "if you have other references to the same object" now, or at some stage in the future. Belts and braces.
I have incorporated the modified script you give above (plus some notes) into the "experimental" part of my main AHK script file, for when I might need it. I too find it easier to run a piece of worked demo code to understand/analyse what it is doing and why.

By the way, I found the link you provided to the discussion thread with Lexikos quite informative, so thanks again.
I am left still curious as to what @pagebooks was intending using the incremental numbering for. I mean, he's presumably wanting to generate numbered lines - but why? I would have thought that was likely to be an already-automated feature in most applications.      :huh:
« Last Edit: June 25, 2012, 07:08 PM by IainB »

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,776
    • View Profile
    • Donate to Member
Re: DONE: Generate sequential serial numbers
« Reply #24 on: June 25, 2012, 07:00 PM »
FWIW, I think there's been a genuine misunderstanding here. My impression is of two people intending to be helpful. I can learn a little something from either side of the discussion, so thank you both.

I don't think it's a matter of IQ. If another is better at something than I, that doesn't necessarily result from a higher IQ than mine (though in my own case it almost certainly does mean that — the "For Dummies" series was clearly written for those like me). It probably means that the other has more or different experience.