Problem to save & get values from hash array

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
barhaqodes
Level 3
Level 3
Posts: 112
Joined: Mon Mar 27, 2023 3:33 pm

Problem to save & get values from hash array

Post by barhaqodes »

Can you please help me to fix this stuff? I want to get the line numbers of each word which I am looking for in php code. Two days I try to correct the code with no correct result.

Expected result:

Code: Select all

WORD:SELECT RESULT:2 searched in sql_array
WORD:FROM RESULT:3 searched in sql_array
WORD:LEFT_JOIN RESULT:6 searched in sql_array
WORD:ON RESULT: searched in sql_array
WORD:WHERE RESULT:8 searched in sql_array
SELECT:2
FROM: 3
LEFT_JOIN: 6
ON: 0
WHERE: 8
Wrong output:

Code: Select all

generuje tento výstup:
WORD:0 RESULT:0 8 3 6 2 searched in sql_array
--------------
SELECT:0 8 3 6 2
FROM: 0 8 3 6 2
LEFT_JOIN: 0 8 3 6 2
ON: 0 8 3 6 2
WHERE: 0 8 3 6 2
Code

Code: Select all

#!/bin/bash

# My function
search_words() {
    local sql_array="$1"
    local query="$2"
    local words=("${@:3}")
    declare -A results=()  # Deklaration of ass. arr.

    sql_array=$(echo "$sql_array" | sed -e 's/^[ \t]*//') # Trim begin of the lines

    # This will create the complete list of words to be found
    local all_words=("$query" "${words[@]}")
    # This will try to find the word in the text and print the current number where found_
    for word in "${all_words[@]}"; do
        result=$(echo "$sql_array" | awk -v word="$word" '$0 ~ word {print NR; exit}')

        if [ -z "$result" ]; then
            result=0
        fi

        # This should save the number under the word like "SELECT" or "INSERT INTO" if that would be the case, WHERE, FROM etc.
        results["$word"]="$result"
    done

    # This should be hash array passed back to global scope
    echo "${results[@]}"
}

clear
# php code - the source of sql_array
sql_array="
    'SELECT'    => 'f.*',
    'FROM'      => array(
        FORUMS_TABLE        => 'f'
    ),
    'LEFT_JOIN' => array(),
    'ORDER_BY'  => 'f.left_id',
    'WHERE'     => 'f.id = 1'
"

# I will look for "SELECT" or other query word + "FROM" "LEFT_JOIN" "ON" "WHERE" 
words=("FROM" "LEFT_JOIN" "ON" "WHERE")

# The function call
results=$(search_words "$sql_array" "SELECT" "${words[@]}")

# Check this
for word in "${!results[@]}"; do
    echo "WORD:$word RESULT:${results[$word]} searched in sql_array"
done

echo "--------------"

# Check this
echo "SELECT:${results[SELECT]}"
echo "FROM: ${results[FROM]}"
echo "LEFT_JOIN: ${results[LEFT_JOIN]}"
echo "ON: ${results[ON]}"
echo "WHERE: ${results[WHERE]}"
You may also want to add this line:

Code: Select all

read -p "WORD:$word RESULT:$result searched in sql_array"
before

Code: Select all

        if [ -z "$result" ]; then
to see that the awk code works very well. I have the result line no. in each cycle like (2 , 3, 6, 0, 8 ):

Code: Select all

WORD:SELECT RESULT:2 searched in sql_array
WORD:FROM RESULT:3 searched in sql_array
WORD:LEFT_JOIN RESULT:6 searched in sql_array
WORD:ON RESULT: searched in sql_array
WORD:WHERE RESULT:8 searched in sql_array
WORD:0 RESULT:0 8 3 6 2 searched in sql_array
So it seems it is not correctly saved to results or I cannot get it back to be accessed by a single word
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: Problem to save & get values from hash array

Post by Shiva »

barhaqodes wrote: Fri Mar 22, 2024 7:26 am Can you please help me to fix this stuff? I want to get the line numbers of each word which I am looking for in php code.
I don't know for other members, but it would, at least for me be easier if I had an example of the original situation (a dummy sql_array) and the output you're looking for, eventually the method because looking for matching lines, grep -n or awk come to mind as something quite obvious.

Example from a function I wrote to get all the lines of file $2 matching pattern $1 :
Step 1 : getting an array of the matching lines.

Code: Select all

readarray -t FLMatchArr < <(grep -n "$2" "$1" | cut -d ':' -f 1 )
Step 2 : "nice" printing with a Delimiter between each number and a dot at the end (FLMatchStr is redirected to a log file in my script). Checks first if there are matching lines.

Code: Select all

FLMatchNb="${#FLMatchArr[@]}"
if [ $FLMatchNb -eq 0 ]
then
	ErrNoMatch # a function designed to manage the absence of matching lines
else
	Del=', '
	FLMatchStr=$(printf "%s$Del" "${FLMatchArr[@]}"; printf "\\b\\b.\n")
fi

Code: Select all

echo $FLMatchStr
will give you a nice output of your matching lines, but Step 1 should already give you what you seem to be looking for.
barhaqodes
Level 3
Level 3
Posts: 112
Joined: Mon Mar 27, 2023 3:33 pm

Re: Problem to save & get values from hash array

Post by barhaqodes »

I have solved the code. The main problem is that results were locally declared instead of globally.
Post Reply

Return to “Scripts & Bash”