ranged arrays 
Author Message
 ranged arrays



Quote:


>>    This and most of the rest work fine, but there's something that makes
>>the whole attempt futile: Instead of passing negative index values to the
>>appropriate methods, perl translates them to non-negatives the way we're
>>used to with arrays starting at 0 by using FETCHSIZE.

>Yes, that's a drag, isn't it?  As i recall this came up on p5p at a
>time and a decision to make it work the way it does was taken.  That
>is, it was not an oversight.

Nick Ing-Simmons says it was not an oversight:

http://www.*-*-*.com/

I now have a patch that allows the use of negative indices in tied
arrays, without breaking backward compatibility, but some of the
DB_File tests are failing when I put it in, so I haven't sent it to
p5p for inclusion.  Here it is, if you are interested:

--- av.c        2001/11/15 15:03:05     1.1

 Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
 {
     SV *sv;
+    unsigned char neg_index = 0;

     if (!av)
        return 0;

     if (key < 0) {
-       key += AvFILL(av) + 1;
-       if (key < 0)
-           return 0;
+      if (SvRMAGICAL(av)) {
+        neg_index = MGp_NEG_INDEX;
+        key += mg_size((SV *) av) + 1;
+      } else {
+        key += AvFILLp(av) + 1;
+      }
+      if (key < 0)
+        return 0;
     }


        {
            sv = sv_newmortal();
            mg_copy((SV*)av, sv, 0, key);
+            SvMAGIC(sv)->mg_private |= neg_index;
            PL_av_fetch_sv = sv;
            return &PL_av_fetch_sv;
        }
--- mg.c        2001/11/15 16:46:02     1.1

     if (n > 2) {
        PUSHs(val);
     }
+    if (mg->mg_private) {
+        EXTEND(SP, 1);
+       PUSHs(newSViv((IV)mg->mg_private));
+    }
     PUTBACK;

     return call_method(meth, flags);
--- mg.h        2001/11/15 16:38:55     1.1

 #define MGf_MINMATCH   1


+#define MGp_NEG_INDEX  1
+
 #define MgTAINTEDDIR(mg)       (mg->mg_flags & MGf_TAINTEDDIR)
 #define MgTAINTEDDIR_on(mg)    (mg->mg_flags |= MGf_TAINTEDDIR)
 #define MgTAINTEDDIR_off(mg)   (mg->mg_flags &= ~MGf_TAINTEDDIR)
--- pod/perltie.pod     2001/11/15 17:09:01     1.1

 If a negative array index is used to read from an array, the index
 will be translated to a positive one internally by calling FETCHSIZE
-before being passed to FETCH.
+before being passed to FETCH and a special third argument will be
+passed to FETCH.  The third argument is a flag vector.  If the low bit
+of this argument is set, it means the original index was negative.
+You can detect and handle negative indices as follows:
+
+    sub FETCH {
+      my $self  = shift;
+      my $index = shift;
+      my $flags = shift;
+      if (defined $flags && $flags & 1) {
+        # the index was originally negative
+        $index -= $self->FETCHSIZE;
+      }  
+      # Now do something with $self and $index.
+    }

 As you may have noticed, the name of the FETCH method (et al.) is the same
 for all accesses, even though the constructors differ in names (TIESCALAR
rd
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print



Wed, 05 May 2004 02:18:02 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Help with multi user app

2. Any Pascal Experts ??

3. ranged arrays

4. range of array indexes

5. Perl puzzle: array of booleans to ranges

6. @array(@range) possible??

7. Array slices using variables for range

8. RFC: Making array using range op and map

9. Thanks and Follow-up (was: RFC: Making array using range op and map)

10. how do I drag from a DbGrid?

11. Storing objects in RDBMS

12. HSB B.V. (NL) looks for: DELPHI/C++ systemdesigners/programmers

 

 
Powered by phpBB® Forum Software