Help with checking that 2 variables contain matching characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with checking that 2 variables contain matching characters
# 1  
Old 09-20-2011
Help with checking that 2 variables contain matching characters

hi
i am writing a hangman script and am having trouble checking the correct letters against the word
i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario

eg

word
hello

guessed
adhewlso

this would be a correct result because all the letters within the word have been guessed

any help would be appreciated im stumped by this
# 2  
Old 09-20-2011
One way:

Code:
$ word=hello
$ guess=adhewlso
$ echo $word | fold -1 | sort -u | awk -v guess=$guess '{ m=match(guess,$0); if ( m == 0 ) { exit 1}}'
$ echo $?
0
$ guess=xyzhel
$ echo $word | fold -1 | sort -u | awk -v guess=$guess '{ m=match(guess,$0); if ( m == 0 ) { exit 1}}'
$ echo $?
1

This User Gave Thanks to clx For This Post:
# 3  
Old 09-20-2011
no SED and AWK

unfortunatly i am not aloud to use the SED or AWK commands for this section therefore although apreciated i can not incorporate your response within my work
thankyou for your time though
# 4  
Old 09-20-2011
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking the user input in perl for characters and length

My question is basically as the title says. How can I check a user inputted string is only certain characters long (for example, 3 characters long) and how do I check a user inputted string only contains certain characters (for example, it should only contain the characters 'u', 'a', 'g', and 'c')... (4 Replies)
Discussion started by: Eric1
4 Replies

2. Shell Programming and Scripting

awk - checking last three characters

Hello, I am working with some very large files (upwards of 1M records). I have written code to parse out a lot of the data and am using awk rather than a built-in "while read LINE" for performance (I have tested both ways). That said, I now need to read each of these incoming lines, check the ninth... (2 Replies)
Discussion started by: dagamier
2 Replies

3. Shell Programming and Scripting

Checking a pattern in file and the count of characters

I am having a zipped file which has the following URL contents - 98.70.217.222 - - "GET /liveupdate-aka.symantec.com/1340071490jtun_nav2k8enn09m25.m25?h=abcdefgh HTTP/1.1" 200 159229484 "-" "hBU1OhDsPXknMepDBJNScBj4BQcmUz5TwAAAAA" "-" In this line here is we only need to consider the... (4 Replies)
Discussion started by: Naks_Sh10
4 Replies

4. Homework & Coursework Questions

Matching and Replacing Characters

I need to write an Unix script to report the number of SQL files in my home directory, ending with .sql . The script should, also, be checking whether there is a file with an underscore in its name. If that is the case, the underscore should be converted to a dash (‐); for example... (1 Reply)
Discussion started by: ronoz-4
1 Replies

5. UNIX for Dummies Questions & Answers

Matching the [] characters

I want to check where our programmers are using "delete" instead of "delete" in their C++ code: grep -r delete *cpp | grep -v However, this statement and variations thereof (escaping with backslash (\), single quotes ('), double quotes ("), accolades ({)) always lead to the following message:... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

checking variables + arithmetic operator help

i'm trying to make a script to simply add numbers together for example i have a file script file called add add 1 2 3 4 5 15 however i want the user to put at least 3 numbers so i did this if then echo "please enter more than two numbers" exit 1 fi but it's telling me i have... (3 Replies)
Discussion started by: el3ctr0nic47
3 Replies

7. Homework & Coursework Questions

Check 2 variables for matching characters

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario this must... (1 Reply)
Discussion started by: lsecer
1 Replies

8. UNIX for Advanced & Expert Users

Checking for certain characters

Could anyone help with the following enquiry.. i have a file in the following format: ID .... VALUE A001 .... 100 B002 .... 200 A004 .... 300 B006 .... 100 A997 .... 200 B776 .... ... (13 Replies)
Discussion started by: SAMZ
13 Replies

9. UNIX for Dummies Questions & Answers

Ksh Checking if string has 2 characters and does not contain digits?

How could I check if a string variable contains at least (or only) 2 characters, and check and make sure that the string does not contain any numeric digits?...I need to know how to do this as simple as possible. and I am using the Ksh shell. Thanks. (1 Reply)
Discussion started by: developncode
1 Replies

10. Shell Programming and Scripting

Checking variables

Firstly - aplogies to Vino I need to check wether or not a variable called DATE is actually a date by doing the following - checking it is all numeric and secondly checking if it is 8 digits long. Any help appreciated (10 Replies)
Discussion started by: penfold
10 Replies
Login or Register to Ask a Question