# Getting the correct index from an array

• 01-04-2013
Garrett85
Getting the correct index from 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.
• 01-04-2013
Miven
You don't need to go
Code:

`\${CALENDER[\${SECONDDIGIT}]}`
You can just go
Code:

`n=1; d=\${CALENDER[n]}`
Which will return d=28 according to the array you presented. And don't forget leap years.:)

But enough of that. I'm assuming you're scripting in bash, and it would *really* help to show a code snippet so I can tell what you're trying to do and where you're going wrong. You have 2 variables, d1 and d2. d1 is 1-9 and d2 is 10-12. Fine. This array has 12 elements, but is numbered 0-11. That's how arrays work. It would seem you need to go 0-8 and 9-11 to index this array properly.

But, again, I'm assuming far too much.

Peace and cheer.
• 01-04-2013
Garrett85
Below is the whole script. And yes, I have accounted for leap year. Thanks.

Code:

```#! /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 ### #################### 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 [ \$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 incalid 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 "\${DD} 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 #}```
• 01-04-2013
watael
Quote:

It would seem you need to go 0-8 and 9-11 to index this array properly.
index 0 can be filled with a fake value[code]array=( _ 31 28 ... ), this way you do not make your code heavier with arithmetics

note that Garrett85 asks here too
• 01-05-2013
Garrett85
note that Garrett85 asks here too

I see you get around lol.