Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error not seen before...please help 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
hello,

A script which has run fine for years has apparently thrown up an error to a member, i've not seen it before and googling didn't help, perhaps someone here can...
Modification of non-creatable array value attempted, subscript -5

what does that mean?

cheers, 1DMF.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
ok realised what it was, subscript -5 actually represented the array index of -5 , which I assume can only be integers!

Accounts had made a typo on the statement date , causing the problem, best catch that error in future!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
You can use negative index numbers in arrays, it counts them from the end rather than the beginning. So an index of -1 would be the last element in the array.

For example:
Code:
my @array = ('a'..'z');
print "Index  5: $array[5]\n";  # prints Index  5: f
print "Index -5: $array[-5]\n"; # prints Index -5: v

That error was probably thrown because the array contained less than 5 elements. But your right, you'll want to watch out for that kind of typo or you could end up with some strange results.
 
You can use negative numbers to access exisitng array elements or assign new values to existing array elements but not to create them.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The reason is obvious after a moment of thought. A negative index is relative to the length of the array, so when you say something like:

$array[-2] = 1;

that would be the second to last value of the array no matter how long it is, but if the array was not already at least two in length, the new element would be before the beginning of the array.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks guys.

They had typed 2000 as the year instead of 2008 , because in the code the oldest date can be 2005 (due to when this part of system was implemented).

So to create dynamic indexes i simply used...
Code:
# Loop Statements and build array for menu structure.
for(@rs){
    # Split date 
    my @sdate = split(/-/,substr($_->{'SDate'},0,10));
    my $thisperiod = "$sdate[1]-$sdate[0]";
    
    # Correct into index value
    my $year = $sdate[0] - 2005;
    my $mon  = $sdate[1] - 1;

    # Set current month/year current period array index
    if($i == -1){$curperiod = "$sdate[1]-$sdate[0]"; $i++;}
    else{if($thisperiod ne $curperiod){$curperiod = "$sdate[1]-$sdate[0]"; $i=0;};}
        
   [b] # insert data into menu array
    $months[$mon][$year][$i] = $_;[/b]
   
    # Add one to period array index
    $i++;        
}

It was balking @ the highlighted section. I've now added error trapping into the MS Access front end so they cannot enter a date < 2005, so it shouldn't happen again!

But at least I now know I can use negative indexes to count backwards through arrays ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top