revjim.net

geeks only

Apparently, this Perl code does something I didn’t realize:

foreach my $thevalue (@some_array) {
	# Do something
}

In this case $thevalue is actually a reference to that index of @some_array. I’m not sure how, in all of the Perl I’ve written, I would have never run into that, and I haven’t looked it up in any documentation, but that seems to be the case. Actually… even in this case, I wouldn’t have run into it except I was helping someone else. To see proof of this (or to see my proof, for you to disprove) look at this code:

my @some_array = ('A123456', 'B123456', 'C123456');

my $cnt = 0;
while($cnt < 2) {

	foreach my $thevalue (@some_array) {
		chop($thevalue);
		print $cnt . ": " . $thevalue . "\n";
	}

	$cnt++;

}

Code untested.

You would think it would print this:

0: A12345
0: B12345
0: C12345
1: A12345
1: B12345
1: C12345

But it doesn't, it prints:

0: A12345
0: B12345
0: C12345
1: A1234
1: B1234
1: C1234

Weird. I don't think it should work that way at all.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>