This is a discussion on OT : Match number-string script ? within the Slackware Linux Support forums, part of the Unix Operating Systems category; --> Hello, I would like to write a short script to check an inputted string of numbers, against a list ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hello, I would like to write a short script to check an inputted string of numbers, against a list of predefined number strings. I have no experience with scripting, if that is indeed the right terminology for what I want to do, so I don't know if it's possible without writing a full-blown program. The intended use of this script would be to check inputted lottery numbers against a list of syndicate-members' chosen numbers. In my case, 15 members. I would be very grateful if someone could direct me to any relevant MAN pages to read, as I really don't know where to start, but will use this 'problem' as a hopefully fruitful learning experience. Would I do something like the following ? : 1) Define each members' set of six numbers as a variable. So, in my case, I'd end up with 15 variables, each consisting of a predefined set of 6 numbers. (Or, have the list of numbers in a file somewhere ?) 2) Set up some kind of input variable (right word?) to accept six numbers, maybe seperated by a space, or a comma, semi-colon or similar. 3) Compare this input variable (?) against the predefined members' sets (or a file), to see if there's at *least* a match of 3 numbers, in any one line, in one or more lines. 4) If a match exists, print the relevant line/s to the screen (stdout?) , with the matching numbers on a new line beneath. 5) If no match exists at all, print a witty comment to the screen, and exit. I apologise for this O/T post. I know it's not really a Slackware-specific question, but to quote the vernacular, you guys/gals are 'teh bomb' when it comes to technical Linux questions. That was a compliment by the way. I will (hopefully) be writing whatever I need to write using Slackware 10. Although I guess whatever the solution is, would work on all Linux distributions. Thanks for your time. Cordially, Kleeb. |
| |||
| Kleeb wrote: > Hello, > > I would like to write a short script to check an inputted string of > numbers, against a list of predefined number strings. I have no experience > with scripting, if that is indeed the right terminology for what I want to > do, so I don't know if it's possible without writing a full-blown program. > > The intended use of this script would be to check inputted lottery numbers > against a list of syndicate-members' chosen numbers. In my case, 15 > members. > ... > I will (hopefully) be writing whatever I need to write using Slackware 10. > Although I guess whatever the solution is, would work on all Linux > distributions. > > Thanks for your time. > http://www.google.com/search?hl=en&i...=Google+Search But in the part about redirection, I think the guy wrote it in jive - he says "stdout 2 file" when he means "stdout to file." I think I should fix that and submit it, becuase it's really, really annoying. Of course, I assume you know how to write a program. :-) FWIW, for an array of lists, my first impulse would have been perl, but I had learned to cut-n-paste perl long before I had ever even heard of bash. Come to think of it, see man regex. Have Fun! Rich |
| |||
| On Mon, 9 Aug 2004, Kleeb wrote: > I would like to write a short script to check an inputted string of > numbers, against a list of predefined number strings. I have no experience > with scripting, if that is indeed the right terminology for what I want to > do, so I don't know if it's possible without writing a full-blown program. sounds simple enough to rough up. > The intended use of this script would be to check inputted lottery numbers > against a list of syndicate-members' chosen numbers. In my case, 15 members. like, assuming your program is named 'checkbet': $ cat chosen.list 11 22 33 44 55 66 6 5 4 3 2 1 10 11 12 13 14 15 $ checkbet chosen.list enter winner: 11 22 13 14 25 26 - you lose, sucker! enter winner: 6 5 4 3 2 1 - holy shit, you won! enter winner: ^D $ > > I would be very grateful if someone could direct me to any relevant MAN > pages to read, as I really don't know where to start, but will use this > 'problem' as a hopefully fruitful learning experience. depends on large number of factors - mainly your chosen programming environment but also your current level of skills. man bash, for details about bash environment, but it's a rough row to hoe to learn it that way. google 'bash tutorial' for introductory information. same for any chosen environment. myself choses bash for simple stuff, or awk for more complex problems, and c if it's got to be fast and/or precise, etc. i suspect improved bash skills would benefit the largest segment of the audience here, so let's play with bash for now. > > Would I do something like the following ? : > > 1) Define each members' set of six numbers as a variable. So, in my case, > I'd end up with 15 variables, each consisting of a predefined set of 6 > numbers. (Or, have the list of numbers in a file somewhere ?) well you could right a script that has variable values hardcoded, eg: B1=( 1 2 3 4 5 6 ) B2=( 6 5 4 3 2 1 ) but that's clumsy to maintain each time you throw together a new pool. so, just put the bets into a file, let's call it 'chosen.list', as per my example above. this allows using a bash structure like: #!/bin/bash read -a -p "enter winner: " winner while read -a bet ; do checkwinner && break ; done < $1 exec $0 $1 this reads a set of numbers into winner, then reads the file (we named on the command line) line by line, until either a winner is found or the file is read through. in order to understand this code, you need to understand bash 'read' with the -a argument to handle variable as an array, -p argument to provide prompt. note the " && break ; " which means if checkwinner returned true, then break out of the while/do/done loop. the 'exec $0 $1' implements another layer of looping by having the script re-invoke itself ... $0 is the name the script was called by on the command line, $1 is the command line argument it was given ... .... assuming we first define a function called checkwinner, which compares bet with winner, prints a result, and exits true if bet is a winner or false if bet is a loser. more on that part below. > > 2) Set up some kind of input variable (right word?) to accept six numbers, > maybe seperated by a space, or a comma, semi-colon or similar. bash array variable is convenient to use here. there are as many ways to accomplish this as there are programmers. > > 3) Compare this input variable (?) against the predefined members' sets (or > a file), to see if there's at *least* a match of 3 numbers, in any one line, > in one or more lines. > 4) If a match exists, print the relevant line/s to the screen (stdout?) , > with the matching numbers on a new line beneath. > 5) If no match exists at all, print a witty comment to the screen, and exit. above example would have you write a "function checkwinner" to do these things. requirement (3) begins to require careful programming. requirement (4) & (5) are trivial. roughing it in some more, we have this: #!/bin/bash function testwinner () { blah blah blah } function checkwinner () { if testwinner ; then echo "- holy shit, you won!" return fi echo "- you lose, sucker!" return false } read -a -p "enter winner: " winner while read -a bet ; do checkwinner && break ; done < $1 exec $0 $1 so we now need a function 'testwinner' which would provide the tests of your requirement 3). note that the values of winner are addressed as a set something like ${winner[*]}, or individually as ${winner[1]}, ${winner[2]}, etc. same for bet, of course. so you can test the first value of each like: function testwinner () { c=0 [ ${winner[1]} == ${bet[1]} ] && c=$( expr $c + 1 ) [ ${winner[2]} == ${bet[2]} ] && c=$( expr $c + 1 ) [ ${winner[3]} == ${bet[3]} ] && c=$( expr $c + 1 ) [ ${winner[4]} == ${bet[4]} ] && c=$( expr $c + 1 ) [ ${winner[5]} == ${bet[5]} ] && c=$( expr $c + 1 ) [ ${winner[6]} == ${bet[6]} ] && c=$( expr $c + 1 ) [ $c >= 3 ] && return true return false } okay. now we are getting closer, and you can see now the complexity of doing the actual testing. there are undoubtably a zillion different and better ways of doing this testwinner function, now that we have it isolated out from the mundane user interface wrapper. but we are also running out of free time and getting into real work, so, i will leave the conclusion as an exercise for the reader. have fun :*) > > I apologise for this O/T post. I know it's not really a Slackware-specific > question, but to quote the vernacular, you guys/gals are 'teh bomb' when it > comes to technical Linux questions. That was a compliment by the way. > > I will (hopefully) be writing whatever I need to write using Slackware 10. > Although I guess whatever the solution is, would work on all Linux > distributions. that's affected by your choice of programming environment. do it in bash (or perl or whatever) and it will work fine on any machine running bash (or perl or whatever) which includes much more than just linux. > > Thanks for your time. what comes around, goes around. pass it on. -- Fire the Liars - Impeach Bush and Cheney! Prosecute the criminals - Indict Rumsfeld and Ashcroft! Corruption starts at the top. William Hunt, Portland Oregon USA |
| |||
| Kleeb <kleeb@kleeb.kleeb> wrote: > Hello, > > I would like to write a short script to check an inputted string of > numbers, against a list of predefined number strings. I have no experience > with scripting, if that is indeed the right terminology for what I want to > do, so I don't know if it's possible without writing a full-blown program. > > The intended use of this script would be to check inputted lottery numbers > against a list of syndicate-members' chosen numbers. In my case, 15 members. Can you give some concrete example? Or, should we just guess? -- William Park <opengeometry@yahoo.ca> Open Geometry Consulting, Toronto, Canada |
| |||
| On Monday 09 August 2004 20:36, William Hunt schrieb : > On Mon, 9 Aug 2004, Kleeb wrote: >> I would like to write a short script to check an inputted string of >> numbers, against a list of predefined number strings. I have no >> experience with scripting, if that is indeed the right terminology for >> what I want to do, so I don't know if it's possible without writing a >> full-blown program. I'm just going to say thankyou very much for the really in-depth tips. I appreciate the time taken, thanks guys. I have some work to do it would appear. I may be some time. Thanks again. Cordially, Kleeb. |
| |||
| -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 In alt.os.linux.slackware, Kleeb dared to utter, > I would like to write a short script to check an inputted string of > numbers, against a list of predefined number strings. That's simple enough, depending on how you seperate those numbers and read them of course. > The intended use of this script would be to check inputted lottery numbers > against a list of syndicate-members' chosen numbers. In my case, 15 members. Then you most likely want to input those numbers into a file. Probably something that looks like this. Any similar file should be suitably easy to parse. # cat bets bob: 1 2 3 4 5 6 sue: 11 22 33 44 55 66 joe: 36 25 14 23 34 45 > I would be very grateful if someone could direct me to any relevant MAN > pages to read, as I really don't know where to start, but will use this > 'problem' as a hopefully fruitful learning experience. # less /usr/doc/Linux-HOWTOs/Bash-Prog-Intro-HOWTO > Would I do something like the following ? : > > 1) Define each members' set of six numbers as a variable. So, in my case, > I'd end up with 15 variables, each consisting of a predefined set of 6 > numbers. (Or, have the list of numbers in a file somewhere ?) As I said above, I've put them in a file. You seem new at this, so storing them in an array just gets hectic. grep'ing plain text files is porbably more familiar for you at this point. It might be a little clunkier this way, but remember these words of ancient wisdom from the Tao: "Though a program be but three lines, one day it must be maintained." A lees elegant program that you understand is going to be something you're happier with. > 2) Set up some kind of input variable (right word?) to accept six numbers, > maybe seperated by a space, or a comma, semi-colon or similar. That's easily enough done. #!/bin/bash echo -n "Enter your name: " read name echo -n "Enter your bet: " read bet echo "${name}: ${bet}" >> /tmp/bets > 3) Compare this input variable (?) against the predefined members' sets (or > a file), to see if there's at *least* a match of 3 numbers, in any one line, > in one or more lines. Oh.... ouch. That gets complicated. You'd probably have to do a while loop for this. Let's see. I'm going to assume that you've going to enter a set of numbers at random and then want to check these numbers for matches. > 4) If a match exists, print the relevant line/s to the screen (stdout?) , > with the matching numbers on a new line beneath. Let's assume you're talking about entering 6 of these randomly chosen numbers. Doing this my way means you'd have to grep /tmp/bets for each number. grep $NUMBER /tmp/bets >> /tmp/hits sort /tmp/hits | uniq -c That little bit will tell you how many times a user matches NUMBER. There's a couple problems with this though. NUMBER would have to be formated like " 4 " to prevent NUMBER from matching 4, 14, 24, 34, etc. Again, this is assuming if you do things with plain text files over arrays. > 5) If no match exists at all, print a witty comment to the screen, and exit. This is left as an excersize to the reader. > I apologise for this O/T post. I know it's not really a Slackware-specific > question, but to quote the vernacular, you guys/gals are 'teh bomb' when it > comes to technical Linux questions. That was a compliment by the way. No trouble. Off-topic technical questions are almost always welcome here. Off-topic flame wars and "what should I call my religion?" belong elsewhere. > I will (hopefully) be writing whatever I need to write using Slackware 10. > Although I guess whatever the solution is, would work on all Linux > distributions. Anything that will run on one linux will run on all of them; it just might require more work than it's worth. - -- It is better to hear the rebuke of the wise, Than for a man to hear the song of fools. Ecclesiastes 7:5 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQFBF/h2lKR45I6cfKARAjjtAKCVc5JEDACTeOgPKZvn7WUBdpidmgCc DP39 /4r1s3+kllvodhk7ENd71WM= =fDP4 -----END PGP SIGNATURE----- |
| |||
| On Mon, 09 Aug 2004 17:21:05 -0500, +Alan Hicks+ schrieb : > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > In alt.os.linux.slackware, Kleeb dared to utter, >> I would like to write a short script to check an inputted string of >> numbers, against a list of predefined number strings. > > That's simple enough, depending on how you seperate those numbers and read > them of course. Thanks Alan. You're right in that scripting is very new to me, but it's something I've wanted to be able to do, at least in a basic way, for some time. If I get this one right, even though it'll probably take me some time to do, the benefits will be worth it. Your examples have been taken onboard. Thanks again. Cordially, Kleeb. |
| |||
| -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 In alt.os.linux.slackware, Kleeb dared to utter, > Thanks Alan. You're right in that scripting is very new to me, but it's > something I've wanted to be able to do, at least in a basic way, for some > time. Let me give you the same advice Faux_Pseudo gave me when I was starting out. Use the console for everything you possibly can, and for every task you find yourself having to do, at least attempt to write a script to do it for you. Soon enough you'll be picking things up, learning what works, what doesn't. > If I get this one right, even though it'll probably take me some > time to do, the benefits will be worth it. Exactly. > Your examples have been taken onboard. Thanks again. Anytime. - -- It is better to hear the rebuke of the wise, Than for a man to hear the song of fools. Ecclesiastes 7:5 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQFBGMOYlKR45I6cfKARAsKlAJ9tlifBXhgNjvzeY6KhGT wvL9KOxACcCosB I1hN7agUEhEjrrpKNF0l3c8= =J0dg -----END PGP SIGNATURE----- |
| |||
| On 10 Aug 2004 07:47:47 -0500, +Alan Hicks+ wrote: >> Your examples have been taken onboard. Thanks again. > > Anytime. Hicks, you have to GOT to stop your raving, malevolent and contumacious vitriol against helpless newbies! I for one am sick and tired of hearing you gripe and moan about being "bothered" helping newbies who should just read the fucking manual! You never offer anything helpful or constructive to this group, and I hope you just crawl under the rock whence you came and restore your head to its rightful place in your rectum. Bryan PS: Do I need to insult anyone's intelligence by placing a smiley? -- No huhu, cobber. bryan (at) hevel (dot) org |
| ||||
| Bryan Bibb wrote: > On 10 Aug 2004 07:47:47 -0500, +Alan Hicks+ wrote: >>> Your examples have been taken onboard. Thanks again. >> >> Anytime. > > Hicks, you have to GOT to stop your raving, malevolent and > contumacious vitriol against helpless newbies! I for one am sick and > tired of hearing you gripe and moan about being "bothered" helping > newbies who should just read the fucking manual! You never offer > anything helpful or constructive to this group, and I hope you just > crawl under the rock whence you came and restore your head to its > rightful place in your rectum. > > Bryan > > PS: Do I need to insult anyone's intelligence by placing a smiley? > OOh! Ooh! Insult me! Insult me! |