www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Access original data from array

reply Moritz Warning <moritzwarning web.de> writes:
Hi,

how can I access the original value for xs?

I assume that xs is allocated at program start,
because I don't get an access violation on gnu/linux
when I reassign values.

But how can I access the original value?
Do I have to keep a copy before xs is modified?

import tango.io.Stdout;


void main(char[][] args)
{
    static char[][2] xs = ["abc", "123"];
    xs[0] = "foo";
    xs[1] = "bar";
    
    foreach(x; xs)
    {
        Stdout(x).newline;
    }

    //how to print "abc" "123" now?
}
Sep 30 2009
parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Moritz Warning wrote:
 Hi,
 
 how can I access the original value for xs?
 
 I assume that xs is allocated at program start,
 because I don't get an access violation on gnu/linux
 when I reassign values.
 
 But how can I access the original value?
 Do I have to keep a copy before xs is modified?
 
 import tango.io.Stdout;
 
 
 void main(char[][] args)
 {
     static char[][2] xs = ["abc", "123"];
     xs[0] = "foo";
     xs[1] = "bar";
     
     foreach(x; xs)
     {
         Stdout(x).newline;
     }
 
     //how to print "abc" "123" now?
 }
You were probably looking for the old meaning of .init, but it's gone now, so I present these alternative fixes: 1) You need to load the state of the game from before overriding xs. I recommend Quick Save and Quick Load. Often bound to F5 and F9. 2) Perhaps a custom-fit Delorian will do. 3) If all else fails, I'm afraid you'll have to resort to copying the contents of xs prior to overwriting them. -- Tomasz Stachowiak http://h3.team0xf.com/ h3/h3r3tic on #D freenode
Sep 30 2009
next sibling parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Tom S wrote:
 2) Perhaps a custom-fit Delorian will do.
DeLorean, even.
Sep 30 2009
prev sibling parent Moritz Warning <moritzwarning web.de> writes:
On Thu, 01 Oct 2009 05:02:10 +0100, Tom S wrote:

 Moritz Warning wrote:
 Hi,
 
 how can I access the original value for xs?
[..]
 
 You were probably looking for the old meaning of .init, but it's gone
 now, so I present these alternative fixes:
 
 1) You need to load the state of the game from before overriding xs. I
 recommend Quick Save and Quick Load. Often bound to F5 and F9.
I tried F5 and F9, but I only accidently activated the bonus level. Quirky game...
 2) Perhaps a custom-fit Delorian will do.
- A FluxCapacitator would be an overkill solution. - I don't do coding while driving at > 88Mph.
 
 3) If all else fails, I'm afraid you'll have to resort to copying the
 contents of xs prior to overwriting them.
I will give it a try. Thanks!
Sep 30 2009