LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-19-2013, 11:14 PM   #16
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333

Did you read my post #7, above? I thought I'd explained (and shown you an example - and function) that did what I think your Python code accomplishes.

At the risk of the pun police calling, "The key is in the indirection operator!"
 
Old 08-20-2013, 01:29 AM   #17
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
I am with PT ... I do follow the python example but believe his solution provided the same result.

Are you having issues when using that example?
 
Old 08-20-2013, 10:07 AM   #18
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
I've got it, I had to adjust it without using shuf because I don't have that command, after reading a post I realized it wasn't too much to rework. Thanks for the help. How did you come up with that logic?

Last edited by Lucien Lachance; 08-20-2013 at 10:51 AM.
 
Old 08-20-2013, 11:19 AM   #19
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Quote:
I had to adjust it without using shuf because I don't have that command
Well that is a little weird as it is a part of the coreutils package, which is pretty standard on most distros, at least that I have used (which is a few )
Quote:
How did you come up with that logic?
Can't obviously speak for PT as the author, but essentially you told us the logic with your python example.

As an alternative to shuf you could combine min86's suggestion with PT's script to get:
Code:
#!/bin/bash

declare -A moves
moves=(['r']="rock" ['p']="paper" ['s']="scissors")
tmp=(${!moves[*]})

for ((i=0;i<10;++i))
do
	key=${tmp[RANDOM % ${#tmp[*]}]}
	echo key=${key}
	echo "move ${i} = ${moves[${key}]}"
done
 
Old 08-20-2013, 05:12 PM   #20
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Quote:
Originally Posted by Lucien Lachance View Post
[...]How did you come up with that logic?
Well, the usual way: I read the info file.

The function was written because I thought you would, perhaps, be using it for several different arrays.
 
Old 08-20-2013, 05:51 PM   #21
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
I wish the man pages weren't always so terse, I have to get the hang of that still. Most of the time I know, or have an idea of how it needs to be solved and go from there. Do you usually know where to begin looking in the docs, or does it usually take you awhile to figure things out? I hope this makes sense..
 
Old 08-21-2013, 10:31 AM   #22
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
The man pages are intentionally terse. They are supposed to be used to "remind" command users about things they seldom use. The info files are (usually) more complete, often with examples. (If you have the pinfo command installed, it makes using the info system much easier and intuitive.)
 
Old 08-21-2013, 08:40 PM   #23
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
I see, I found this helpful link as well concerning lower case and upper case submissions: http://mintaka.sdsu.edu/GF/bibliog/l...bian/bash.html
Really puts things into perspective, I had always thought using tr, awk, or perl were the only ways of achieving this. Thank you pointing out that bash could handle this problem natively @grail.
 
  


Reply

Tags
bash, linux, shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Script and $RANDOM lupusarcanus Programming 2 03-15-2011 03:24 AM
[SOLVED] Bash Problems - Random Headers when starting bash in X androidd Slackware 3 01-13-2011 05:29 PM
random sticky keys and mouse clicks in X Bogus8 Linux - Laptop and Netbook 2 01-21-2007 12:46 AM
random keys not working walden Slackware 4 02-16-2005 06:55 PM
bash random function NSKL Programming 6 02-02-2003 02:39 PM


All times are GMT -5. The time now is 06:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration