LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 12-11-2019, 12:52 PM   #16
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196

"Didn't work" is not a useful description of what you have tried and what the results were. Posting whole cloth code repeatedly also tends to cause others to look elsewhere.

Have you actually read and tried to understand how that code is working? Look at the last line for one big clue as to why it may not be decoding that string.

I see also that you have changed the name of the crypt array from the original script, reversed the keys and values, which makes no sense with your subsequent exchange of keys and values using the same array (and won't work either!). Think about how that was working in the first test used with the encode function, then think about what it must do to allow you to reverse the process with the decode function... learn!

Last edited by astrogeek; 12-11-2019 at 01:05 PM.
 
Old 12-11-2019, 01:41 PM   #17
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
it's only been 2 weeks since i'm learning shell script
it's my first programming language
forgive my ignorance

just wanted to make it work
 
Old 12-11-2019, 02:01 PM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
if you have just started you need to try something easier, take a simpler task.
If someone posts the solution you will not understand a line. What is your goal?
Or is this your homework?
 
Old 12-11-2019, 02:40 PM   #19
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
I'm learning on my own through books
I like solving ciphers and I thought something like this to decipher
I didn't find any google search on how I could do this
I'm trying to understand the code, I know it has the array, the "for" is loop and "while" too,
I really wanted to be able to solve this
 
Old 12-11-2019, 03:35 PM   #20
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
It is OK, no one is criticizing you, we want you to learn it!

Please read my previous post and consider the two main points I made:

* If you are trying to decode, should you change the function being called?
* If the script reverses the crypt array for use in the decode function, why would you rewrite it in reverse yourself?

The main point is this: If you want to understand how it works, you need to read it line by line and try to understand what exactly each line is doing for you. Don't make any changes to the code until you can explain why they are needed and what the change will do for you.

Making changes on top of changes which you do not understand leads nowhere.
 
1 members found this post helpful.
Old 12-11-2019, 04:04 PM   #21
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
Talking

Code:
#!/usr/bin/env bash

declare -A decrypt=(
    [AAAAAAAA]="A"
    [BBBBBBBB]="B"
)

decode () {
    local word=$1
    for ((i=0; i<${#word}; ++i)) ; do
        local char=${word:$i:8}
        printf %s'' ${decrypt[$char]}
    done
    printf '\n'
}

decode 'AAAAAAAABBBBBBBB'
I did it!
I did it!
I did it!
Hurray! Hurray! Hurray!

I just had to add number 8
YEAHHHHHH
 
Old 12-11-2019, 04:08 PM   #22
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
pan64, grail, Turbocapitalist, danielbmartin, dugan, astrogeek
thanks to everyone

Last edited by Pinguino99; 12-11-2019 at 04:12 PM.
 
Old 12-12-2019, 06:37 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I would also point out that not ALL of your key/value pairs had char/8 chars as some had 9
Code:
    [' ']="111_space"
    ['!']="life_9900"
So this is either an oversight or will cause your idea of grabbing 8 char segments not work.

Also, your original example pointed to the fact that an already encrypted string would come in as separated items:
Code:
decode '@_banana !!_stars computer life_9900'
So here there would be no need to find every 'x' char grouping as they are already separated into segments which bash can work with.


What I am trying to point out here is that you need to be clear in your original problem proposal, otherwise you are overcomplicating the problem and
the solution required.

Now that you berlieve you have a solution, would you repost with the correct arrays and code so others may learn from you?
Might also be nice to re-enter the original problem spec so people can follow why your solution is correct.
 
1 members found this post helpful.
Old 12-12-2019, 10:02 AM   #24
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
Code:
#!/usr/bin/env bash

declare -A encrypt=(
    [A]="AAAAAAAA"
    [B]="BBBBBBBB"
    [' ']="-0-0-0-0"
    ['!']="!!!!!!!!"
)

declare -A decrypt=(
    [AAAAAAAA]="A"
    [BBBBBBBB]="B"
    ['-0-0-0-0']='-0-'
    ['!!!!!!!!']="!"
)

encode () {
    local word=$1
    for ((i=0; i<${#word}; ++i)) ; do
        local char=${word:$i:1}
        printf %s ${encrypt[$char]}
    done
    printf '\n'
}

decode () {
    local word=$1
    for ((i=0; i<${#word}; ++i)) ; do
        local char=${word:$i:8}
        printf %s ${decrypt[$char]}
    done
    printf '\n'
}

echo "encrypt"
encode 'AB !'

echo "decrypt"
decode 'AAAAAAAABBBBBBBB-0-0-0-0!!!!!!!!'
yes it only reads 8 characters or 1, I have little knowledge in shell script so, i don't know how to make ${word} recognize any number of characters
the solution I found was to adapt the cipher
 
Old 12-12-2019, 11:21 AM   #25
Pinguino99
Member
 
Registered: Dec 2019
Posts: 44

Original Poster
Rep: Reputation: Disabled
Code:
#! /bin/bash
declare -A crypt=(
    [A]="99_banana"
    [a]="@_melon"
    [B]="22_GRAPE"
    [b]="orange"
    [C]="Strawberry"
    [c]="life"
    [' ']="space"
    ['@']="12399"
    ['!']="tv_12"
)

encode () {
    local word=$1
    for ((i=0; i<${#word}; ++i)) ; do
        local char=${word:$i:1}
        printf %s ${crypt[$char]}
    done
    printf '\n'
}

declare -A decrypt
for char in "${!crypt[@]}" ; do
    key=${crypt[$char]}
    decrypt[$key]=$char
done

decode () {
    local word=$1
    while [[ $word ]] ; do
        local code
        for code in "${!decrypt[@]}"; do
            if [[ $word == "$code"* ]] ; then
                printf %s "${decrypt[$code]}"
                word=${word#"$code"}
            fi
        done
    done
    printf '\n'
}

encrypt=$(encode 'Abc !')

decrypt=$(decode "$encrypt")

echo "$encrypt"

echo "$decrypt"
working perfect
 
Old 12-12-2019, 11:28 AM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
So now that you have it working, don't forget to mark the thread as SOLVED

Here was my take on the bash solution which is mostly like yours but with a single array:
Code:
#!/usr/bin/env bash

declare -A gA_cipher

gA_cipher=( [A]="@_banana"
         [a]="99_melon"
         [B]="AZ_GRAPE"
         [b]="!!_stars"
         [C]="81_xxxxx"
         [c]="computer"
         [ ]="11_space"
         [!]="life_900"
       )

usage()
{
  echo "Correct usage:- ${0##*/} [-e|-d] strings"
  exit 1
}

decrypt()
{
  local ls_words
  local ln_num_chars ln_cnt
  local lb_not_found
  local lc_char

  ls_words=$*
  ln_num_chars=8

  for ((ln_cnt = 0; ln_cnt < ${#ls_words}; ln_cnt = ln_cnt + ln_num_chars ))
  do
    for lc_char in "${!gA_cipher[@]}"
    do
      if [[ "${gA_cipher[$lc_char]}" == "${ls_words:ln_cnt:ln_num_chars}" ]]
      then
        echo -n "$lc_char"
        lb_not_found=false
        break
      else
        lb_not_found=true
      fi
    done
    $lb_not_found && echo -n "##no_value##"
  done
}

encrypt()
{
  local ls_words
  local ln_cnt

  ls_words=$*

  for ((ln_cnt = 0; ln_cnt < ${#ls_words}; ln_cnt++ ))
  do
    if [[ "${gA_cipher[${ls_words:ln_cnt:1}]}" ]]
    then
      echo -n "${gA_cipher[${ls_words:ln_cnt:1}]}"
    else
      echo -n "##no_value##"
    fi
  done
}

(( $# < 2 )) && usage

case $1 in
  -d) decrypt "${@:2}";;
  -e) encrypt "${@:2}";;
   *) usage;;
esac

echo
 
1 members found this post helpful.
  


Reply



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
gcompris rocks! missing assetml-voices-alphabet-en soylentgreen Linux - Software 0 09-18-2006 10:37 AM
Fonts: phonetic alphabet letters problem tramni1980 Slackware 1 08-19-2006 05:33 AM
Why do Web addresses and files always use latin alphabet? General General 1 07-04-2006 11:25 AM
French accents/European Alphabet error with Apache/PHP rmanocha Programming 5 07-12-2004 07:52 AM
Bug A Javascript that will not allowed alphabet character type into a text box Linh Programming 2 09-22-2003 11:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:09 AM.

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
Open Source Consulting | Domain Registration