Forum Moderators: coopster & phranque

Message Too Old, No Replies

What's happening to element "0"?

Simple array list

         

rocknbil

8:05 pm on Aug 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@uploads = ('agent_photo', 'logo_graphic', 'photo1', 'photo2', 'photo3', 'photo4');
for $i (0..$#uploads) { $debug .= "i $i ui $uploads[$i] "; }

print $debug;

produces

i 0 ui i 1 ui logo_graphic i 2 ui photo1 i 3 ui photo2 i 4 ui photo3 i 5 ui photo4

Yeah, I know, it SHOULDN'T. element 0 should be 'agent_photo'.

No other references to @uploads anywhere else. No shift, splice, pop, no reference even to a scalar called $uploads. What would cause element 0 to cease to exist? This simple problem is driving me bananas, anyone got any ideas?

EDIT: foreach $u (@uploads) produces the same result. Weird . . .

ChadSEO

8:28 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



rocknbil,

Very strange indeed. I copied your code exactly, on Linux and Windows, and got the proper output both times. I have no idea what the problem is, but I'm definitely going to watch this thread to find out.

Chad

KevinADC

8:37 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



works fine for me too. Double-check the code you are working with, the posted code is fine.

wruppert

9:05 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



Also try "use strict; use warnings;".

rocknbil

9:23 pm on Aug 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is soooo embarrassing. :-)

I'm in the habit of restricting variables to subroutines religiously using the my keyword. So much that I forget I even do it. But this is an old dusty script that wasn't restricting variables.

foreach $pic (@uploads) {
-- upload
--resize
}

The resize routine uses imageMagick and ALSO the var $pic, then undefines it when done.

DOH!

It threw me off badly because there was no reference to @uploads anywhere. Added the my lists to all the subs and poof the problem went away (of course.)

<welcomes the perl boot from all>

Thanks for looking everyone! :-)

coopster

2:43 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No worries, happens to everyone. The funny thing is, I tested your script and it wouldn't work for me until I initialized the variables with "my" prior to the code execution. I thought, "nah, he had to have already been doing this in the code prior somewhere." So I didn't mention it. Here nor there, glad you got it sorted.