LinuxQuestions.org
Help answer threads with 0 replies.
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 01-04-2013, 04:06 PM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Rep: Reputation: 6
shell script indexing an array


I've got an array CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)

I have two if statements that Need to get an index from the array above but when I feed a value into the brackets e.g.
${CALENDER[${SECONDDIGIT}]}
or
${CALENDER[${FIRSTDIGIT}]}

it seems to only be getting the first inded of the CALENDER array, 31, even though FIRSTVALUE will have a number from 1-9 in it and SECONDDIGIT will have 10, 11, or 12. Any ideas by I am unable to get the correct index with my FIRST and SECONDDIGIT values? Thanks.
 
Old 01-04-2013, 04:23 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It should work, except that arrays indexes are numbered from zero:
Code:
$ CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)
$ for INDEX in {0..11}
> do
>  echo ${CALENDER[$INDEX]} 
> done
31
28
31
30
31
30
31
31
30
31
30
31
$ 
 
Old 01-04-2013, 05:00 PM   #3
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Original Poster
Rep: Reputation: 6
Getting the right index from an array

This looks like a simple loop although I'M new to bash. In my case the index number stored in FIRSTDIGIT & SECONDDIGIT will be bases on user input.


Quote:
Originally Posted by colucix View Post
It should work, except that arrays indexes are numbered from zero:
Code:
$ CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)
$ for INDEX in {0..11}
> do
>  echo ${CALENDER[$INDEX]} 
> done
31
28
31
30
31
30
31
31
30
31
30
31
$ 
 
Old 01-04-2013, 05:16 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Garrett85 View Post
In my case the index number stored in FIRSTDIGIT & SECONDDIGIT will be bases on user input.
How exactly the two variables are assigned? I mean, what is the difference between the two from the user point of view?

By the way, if the input is a number from 1 to 12, you can subtract 1 from it:
Code:
FIRSTDIGIT=$((FIRSTDIGIT - 1))
or simply
Code:
((FIRSTDIGIT--))
or even
Code:
let FIRSTDIGIT=FIRSTDIGIT-1
Another way is to add a dummy value at the beginning of the array so that index 0 can be safely ignored:
Code:
CALENDER=(99 31 28 31 30 31 30 31 31 30 31 30 31)
 
Old 01-04-2013, 05:47 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Can you post your code? I suspect that you want to use associative arrays, not numeric indexed ones. Look at the discussion of different array types in pinfo bash for more information.
 
Old 01-04-2013, 07:23 PM   #6
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Original Poster
Rep: Reputation: 6
I main problem I'M having is that the value in the index varibles FIRSTDIGIT & SECONDDIGIT are not getting back the correct index from the CALENDER array. I believe that they are always getting the first index in the array. When I run the script with #! /bin/bash -x I see that the indexing varaible is always being checked agaisnt 31, never 28 or 30. If I add a +1 to the FRISTDIGIT it will then be run again 28 which to me is further proof that it the script doesnt seem to care what the value of the indexing variable is but is always going to the first position in the array.
If I run date 04301998 it fails and I don't know why. Below is my code:
Quote:
#! /bin/bash -x


# This is the calender array for the DateValidation program
CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)


#############################
### FUNCTION-is_leap_year ###
#############################

is_leap_year()
{
PART1=`expr ${YY} % 4`
PART2=`expr ${YY} % 100`
if [[ 0 -eq ${PART1} ]] && [[ 0 -ne ${PART2} ]] || [[ 0 -eq `expr ${YY} % 400` ]]; then #{
result=true
else
result=false
fi #}
}


####################
### END FUNCTION ###
####################

########################
### FUNCTION-get_day ###
########################

get_day()
{
DAY="BAD"
FIRSTDIGIT=${MM:0:1}
SECONDDIGIT=${MM:1:1}
if [[ ${FIRSTDIGIT} -eq 0 ]]; then #{
if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER[${SECONDDIGIT+1}]} ]]; then #{
DAY="GOOD"
fi #}
else
if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER[${MM}]} ]]; then #{
DAY="GOOD"
fi #}
fi #}
}

####################
### END FUNCTION ###
####################

KEEPGOING=1
while [[ ${KEEPGOING} != "exit" ]]; do

read -p "Enter a date for validation: " DATE

# establish the varible LEN to hold the number of characters in Date, 8 is the only valid number
LEN=$(echo ${#DATE})

if [[ "${DATE} == exit" ]]; then
${KEEPGOING}=0

elif [ $LEN -eq 8 ]; then #{

# set date dariables MM, DD, & YY
MM=${DATE:0:2}
DD=${DATE:2:2}
YY=${DATE:4:4}

if [ ${YY} -gt 0 ]; then #{
if [ ${MM} -gt 0 ] && [ ${MM} -lt 13 ]; then
if [ ${MM} -eq 02 ]; then

is_leap_year ${YY}
if [ $result == true ]; then
if [ ${DD} -gt 0 ] && [ ${DD} -le 29 ]; then
echo "${DATE} is a valid date!"
else
echo "${DD} is invalid for this date!"
fi #}
else
get_day ${DD}
if [ ${DAY} == GOOD ]; then
echo "${Date} is a valid date!"
else
echo "${DD} is invalid for this date!"
fi #}
fi #}
else
get_day ${DD}
if [ ${DAY} == GOOD ]; then
echo "${DATE} is a valid date!"
else
echo "${DD} is invalid for this date!"
fi #}
fi #}

else
echo "${MM} is invalid for this date!"
fi #}

else
echo "${YY} is invalid for this date!"
fi #}

else
echo "Invalid number of digits for a date!"
fi #}
done
 
Old 01-05-2013, 08:42 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well there are several other errors with the code, but the error with the array is as follows:
Code:
 ${CALENDER[${SECONDDIGIT+1}]}
Here you have included the increment inside the variable call and hence it is being set to 0 and using the first value.

You can simplify this to:
Code:
${CALENDER[SECONDDIGIT-1]}
I would also urge you to look at (()) for testing numbers and review your exit process which does not work.
 
1 members found this post helpful.
Old 01-05-2013, 01:01 PM   #8
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Original Poster
Rep: Reputation: 6
Thanks grail

Thanks grail. ${CALENDER[SECONDDIGIT-1]} did it rather than my old ${CALENDER[${SECONDDIGIT-1}]} script. That's been driving me mad for days now. Thanks again.

Quote:
Originally Posted by grail View Post
Well there are several other errors with the code, but the error with the array is as follows:
Code:
 ${CALENDER[${SECONDDIGIT+1}]}
Here you have included the increment inside the variable call and hence it is being set to 0 and using the first value.

You can simplify this to:
Code:
${CALENDER[SECONDDIGIT-1]}
I would also urge you to look at (()) for testing numbers and review your exit process which does not work.
 
  


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
Create Array in Shell Script dnoob Programming 13 10-20-2013 04:25 PM
array in shell script Abid Malik Programming 2 10-16-2010 01:49 PM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
Array shell script pooppp Linux - Networking 6 08-01-2008 08:37 AM
Needs to know hashing functions used for array indexing ahm_irf Programming 2 03-04-2008 07:12 PM

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

All times are GMT -5. The time now is 10:37 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
Open Source Consulting | Domain Registration