473,320 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Arrays and references

TMN
Hi

I would like to append stuff to an array but doing it by reference -
this is not working - can anyone help ?

$anArray[]= "";

$ref = &$anArray[];

$ref = "a";
$ref = "b";
$ref = "c";

anArray now contains a,b and c

Thanks
Tim

Feb 15 '07 #1
7 1177
Rik
On Thu, 15 Feb 2007 15:52:28 +0100, TMN <na******@gmail.comwrote:
I would like to append stuff to an array but doing it by reference -
this is not working - can anyone help ?

$anArray[]= "";

$ref = &$anArray[];

$ref = "a";
$ref = "b";
$ref = "c";

anArray now contains a,b and c
This cannot be done like that. $anArray[] is not 'the mystical place where
variables go to be added to an array'. It's a construct that appends
something as a last item to an array, and any reference to it will result
in the actual value added at that point, not a 'new' array value.

Just use $anArray[] = 'foo';, or possibly:

$mainArray = array('foo' =bar,'foz'= array());
$anArray = &$mainArray['foz'];
$anArray[] = 'foo';
$anArray[] = 'bar';

Or even the array_push()/array_unshift() functions.
What is the actual problem you're trying to solve here?
--
Rik Wasmus
Feb 15 '07 #2
TMN wrote:
$anArray[]= "";

$ref = &$anArray[];

$ref = "a";
$ref = "b";
$ref = "c";
$anArray[]= "";
$ref = &$anArray[];
$ref[] = "a";
$ref[] = "b";
$ref[] = "c";

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 15 '07 #3
TMN
On Feb 15, 6:40 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
TMN wrote:
$anArray[]= "";
$ref = &$anArray[];
$ref = "a";
$ref = "b";
$ref = "c";

$anArray[]= "";
$ref = &$anArray[];
$ref[] = "a";
$ref[] = "b";
$ref[] = "c";

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
What I am trying to do is get a reference to an array so I can add
elements to arrays via the ref.

tim

Feb 16 '07 #4
"TMN" <na******@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
On Feb 15, 6:40 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
>TMN wrote:
$anArray[]= "";
$ref = &$anArray[];
$ref = "a";
$ref = "b";
$ref = "c";

$anArray[]= "";
$ref = &$anArray[];
$ref[] = "a";
$ref[] = "b";
$ref[] = "c";

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

What I am trying to do is get a reference to an array so I can add
elements to arrays via the ref.

You have to use the [] construct, there's no way around it. Yo can create a
reference to the array, but you still need the [].

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Feb 16 '07 #5
TMN
$anArray[]= "";
$ref = &$anArray[];
$ref[] = "a";
$ref[] = "b";
$ref[] = "c";

But with this I am ending up with an array inside $anArray not just
a,b and c ?

Thanks
Tim

Feb 16 '07 #6
"TMN" <na******@gmail.comwrote in message
news:11*********************@t69g2000cwt.googlegro ups.com...
$anArray[]= "";
$ref = &$anArray[];
$ref[] = "a";
$ref[] = "b";
$ref[] = "c";

But with this I am ending up with an array inside $anArray not just
a,b and c ?
$anArray = array();
$ref = &$anArray; // No [] here

$ref[] = "a";
$ref[] = "b";
$ref[] = "c";

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Feb 16 '07 #7
TMN
On Feb 16, 3:51 pm, "Kimmo Laine" <s...@outolempi.netwrote:
"TMN" <nash....@gmail.comwrote in message

news:11*********************@t69g2000cwt.googlegro ups.com...
$anArray[]= "";
$ref = &$anArray[];
$ref[] = "a";
$ref[] = "b";
$ref[] = "c";
But with this I am ending up with an array inside $anArray not just
a,b and c ?

$anArray = array();
$ref = &$anArray; // No [] here

$ref[] = "a";
$ref[] = "b";
$ref[] = "c";

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpkhttp://outolempi.net/ahdistus/- Satunnaisesti päivittyvä nettisarjis
s...@outolempi.net | rot13(x...@bhgbyrzcv.arg)
Thank you thank you !!!!!!!
Tim

Feb 16 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are...
5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
8
by: Greg | last post by:
In VB6 I made heavy use of control arrays I see they have been 'deprecated' in vb.Net, with a questionable explanation that they are no longer necessary which just addresses the event issue!...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
3
by: alcabo | last post by:
Hello, I'd like to improve several critical routines involving arrays (vectors and matrices)... How are arrays stored in memory? Row major or column major? (Like in C or like Fortran?)
13
by: sonjaa | last post by:
Hi I'm new to programming in python and I hope that this is the problem. I've created a cellular automata program in python with the numpy array extensions. After each cycle/iteration the...
9
by: Jack | last post by:
If I don't specify "ref" in the argument list when passing an array to the callee, I am passing the array (reference) by value. But this makes me confused because it actually means a "reference" of...
10
by: John | last post by:
I have two separate arrays A and B. The comparison function only depends on elements in A (keys are in A), but the swap() function needs to swap not only A,A, but also B,B ; whenever it swaps A,A....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.