Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > 0th positin set when futzing with $[

Reply
Thread Tools

0th positin set when futzing with $[

 
 
Paul Lalli
Guest
Posts: n/a
 
      08-03-2006
First, I know, - I *know* - one should not play with the $[ variable,
and I would never ever do so in "real" code. Another thread mentioned
the variable, and I was bored, so I started playing. That being said,
I see the following results:

$ perl -le'
$[ = 7;
@foo = qw/alpha beta gamma/;
print "$_ => $foo[$_]" for 0 .. $#foo;
'
0 => alpha
1 =>
2 =>
3 =>
4 => alpha
5 => beta
6 => gamma
7 => alpha
8 => beta
9 => gamma

I can understand positions 7-9 being set. 7 is the first position of
the array now. And I can understand position 4-6 being set. This is
for the "wraparound" feature that we normally see when $[ hasn't been
altered and we access negative indices . But can anyone explain to me
why the position 0 is set? (and yes, I know -w would give me three
"use of uninitialized" warnings for 1-3 above. Not relevant to my
question, so I omitted it).

I do not see any similar results when printing the values of, say -10
through 3 if $[ has not been altered. So I assume this is specific to
the 0th position, after $[ has been modified...

This is perl, v5.8.4 built for sun4-solaris

Paul Lalli

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      08-03-2006

Quoth "Paul Lalli" <(E-Mail Removed)>:
> First, I know, - I *know* - one should not play with the $[ variable,
> and I would never ever do so in "real" code. Another thread mentioned
> the variable, and I was bored, so I started playing. That being said,
> I see the following results:
>
> $ perl -le'
> $[ = 7;
> @foo = qw/alpha beta gamma/;
> print "$_ => $foo[$_]" for 0 .. $#foo;
> '
> 0 => alpha
> 1 =>
> 2 =>
> 3 =>
> 4 => alpha
> 5 => beta
> 6 => gamma
> 7 => alpha
> 8 => beta
> 9 => gamma
>
> I can understand positions 7-9 being set. 7 is the first position of
> the array now. And I can understand position 4-6 being set. This is
> for the "wraparound" feature that we normally see when $[ hasn't been
> altered and we access negative indices . But can anyone explain to me
> why the position 0 is set? (and yes, I know -w would give me three
> "use of uninitialized" warnings for 1-3 above. Not relevant to my
> question, so I omitted it).
>
> I do not see any similar results when printing the values of, say -10
> through 3 if $[ has not been altered. So I assume this is specific to
> the 0th position, after $[ has been modified...


Yup. One thing you didn't try was

-4 =>
-3 => alpha
-2 => beta
-1 => gamma

(with $[=7). The relevant code is in pp_hot.cp_aelem

if (elem > 0)
elem -= PL_curcop->cop_arybase;

where elem is the index requested and PL_curcop->cop_arybase is the
value $[ had when the current statement was compiled (you realise, I
presume, that $[=foo; statements are not ordinary variable assignments,
and are processed more like 'use arybase foo;'? In particular, their
effect is compile-time and lexically scoped).

That line (well, the 'if (elem > 0)' line) has been there since the
initial 5.003 checkin when p5p moved over to Perforce, so I can't get a
commit log to see why it's there...

WRT the original thread, I think that this carping on about how @a - $#a
is not necessarily 1 because of $[ is seriously unhelpful. Noone in
their right mind would use $[ in real code, and it must just confuse
beginners more to have some weird $[ thrown at them when they're not
understanding scalar @a vs. $#a yet.

Ben

--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
http://www.velocityreviews.com/forums/(E-Mail Removed) Frank Herbert, 'Dune'
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid.PageIndexChanged not firing on when 0th page requested Marc Woolfson ASP .Net Datagrid Control 4 05-25-2007 11:59 AM
how to create a node set of elements through C++ code while executing a style sheet and process the created node set. pvssvikas@gmail.com XML 0 01-25-2006 12:48 PM
Treeview questions - how to set set start node and how to catch click event Alan Silver ASP .Net 0 12-21-2005 10:40 AM
java.lang.Set with elements of type java.lang.Set Harald Kirsch Java 4 08-31-2004 10:40 AM
Unable to set focus to textfield in a applet if browser is set to Sun JRE 1.4 Manav Java 0 10-15-2003 03:42 PM



Advertisments