IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New some answers
my @A = ("aaa", "aaa", ("bbb", "bbb"));

is equivilent to
my @A = ("aaa", "aaa", "bbb", "bbb");
what you probably want is
my @A = ("aaa", "aaa", ["bbb", "bbb"]);
This will create an array with three elements: 'aaa', 'aaa' and an anonymous array containing the two elements 'bbb' and 'bbb'.

Perhaps [link|http://www.oreilly.com/catalog/advperl/excerpt/ch01.html|this chapter] will help.
Have fun,
Carl Forde
New OK, another arbitrary distinction to remember
I guess Larry Wall is a linguist - must be an English language linguist. Just enough rules to make exceptions really hurt.

Here is another question: when we take a reference with \\@xxx syntax, do we take the reference to xxx variable or to the data structure it contains? In other words, say we have this piece of code:

@xxx = (1, 2, 3);
$rx = \\@xxx;
@xxx = (9,8,7);
print($rx->[0] . "\\n");

What gets printed? I'd like to guess that it's "1", but I am just confused enough to be unsure.
--

This guy's ahead of his time! He's using quantum programming methods: in universes where invalid data is passed to this function, it does not return. Thus you are ensured that you will only have valid data after calling it. Optimally you'd destroy the universe on failure, but computers haven't quite advanced to that level yet.

-- [link|http://thedailywtf.com/archive/2004/10/26/2920.aspx|The] Daily WTF

New It's easy enough to test...
Here is another question: when we take a reference with \\@xxx syntax, do we take the reference to xxx variable or to the data structure it contains? In other words, say we have this piece of code:

@xxx = (1, 2, 3);
$rx = \\@xxx;
@xxx = (9,8,7);
print($rx->[0] . "\\n");

What gets printed? I'd like to guess that it's "1", but I am just confused enough to be unsure.


the question you're asking is if once you've created the reference if you change the data that the reference was accessing, does it maintain the original data?

A better question might be : how do you gain access to the data to change it?

Compare it to :
   @xxx = (1,2,3);
   $rx = \\@xxx;
   my @xxx = (9,8,7);
   print ($x->[0] . "\\n");


New Yes it is easy to test.
I want a lojical explanation, not "look up this", "test that". Don't you think it's abysmal when such a fundamental and simple question gets the answer along the lines of "test it"?

The answer is "9".

Sooooo. $rx refers to the _variable_name_ @xxx.

It's not a pointer, not really. It's more like a true C++ reference. In C++, there is a world of difference between

char *a = b;

and

char *&a = b;

On the other hand, when we do $rx = [1,2,3]; it behaves like true pointer.

There is a simple rule in there, just waiting to pop out. I can't quite grasp it. My stream of consciousness follows.

I guess the point would be that in Perl, variables are not names of (pointers to) memory areas, but rather "resizable" and type-safe memory areas themselves. It's like having a virtual machine where the word at a given address can contain arbitrary-size array, or either string or number - no matter. The address (variable name) stays the same.

On the other hand, the "kind" of variable cannot be changed (array, hash, scalar, typeglob(?))

In this context, the difference between [] amd () becomes very interesting... I have a feeling that one of them is a literal, the other an operator. Or something.



OK, I am rambling. But I think that the learned company here is pushing me in the right direction. Thank you.
--

This guy's ahead of his time! He's using quantum programming methods: in universes where invalid data is passed to this function, it does not return. Thus you are ensured that you will only have valid data after calling it. Optimally you'd destroy the universe on failure, but computers haven't quite advanced to that level yet.

-- [link|http://thedailywtf.com/archive/2004/10/26/2920.aspx|The] Daily WTF

New Testing has some disadvantages....
...it's possible that a particular piece of code may be ambigious - meaning that the compiler writer can choose how to implement it.

So, simply "testing it" isn't always a solution.

However, in this case, the question of what is happening (and why) require first understanding what did happen. (Thus, "test it")

My rambling non-official option:

  • scalars are probably implemented as pointers (they point to something) with logic to do necessary things, such as convert between integer and characters on the fly
  • arrays are a structure, with each element being a scalar.
  • hashes are a structure (same as array), except that key and value are scalars


So...an array and a hash cannot be a member of an array or hash. (Back to that whole reference thing).

@xxx = (1, 2, 3); --- creates an array xxx and give its the values to the list (1,2,3) (lists and arrays are different - lists are a collection of scalars, lacking the structure of an array. ie: can't get a size of a list, iirc)

$rx = [1,2,3]; --- the [] create an anonymous array (ie: this IS an array). and allows the $rx to reference this array.

so @xxx = (1,2,3), $rx = \\@xxx; is very different from @xxx = (1,2,3), $rx = [1,2,3]; (there are 2 arrays created in the 2nd example).

now...to really play with your mind, 2 things....

  1. You can create N number of references to a variable...

    $reference4 = \\\\\\\\"hello!";

    How to reference this reference to a reference to a reference?
    print $$$$$refenence4;

  2. Perl also has Symbolic references -
    $variablex = 1;
    $symbolic_x = "variablex";

    print $$symbolic_x;







New It isn't arbitrary
You're just not paying attention to the grammar of the language, so it looks arbitrary. If you ignore the grammar of English you'll constantly get tripped up as well. That's life. If you approach Perl saying, "Here's how it should work", you'll be disappointed and shortly after that very frustrated. If you approach it asking, "How does it work?" you'll find that there are rules and it really works according to them.

In Perl there are two basic contexts. List context. And scalar context. In list context any data structure will try to produce a flat list. In scalar context you'll get one thing. The distinction is entirely grammatical. Generally assign stuff somewhere that takes a list, and you get list context. Pass data into a function and you'll get list context. Assign it to a scalar and you'll get scalar context.

So when you see my @A = ("aaa", "aaa", ("bbb", "bbb")); what happens is that there is a list context imposed on the RHS (you're assigning to an array, which takes a list of things) so the RHS is flattened out into a list. Namely ("aaa", "aaa", "bbb", "bbb"). Nothing strange going on with data structures. It is all a question of what the grammar says.

Now to answer the question you had here, the \\ operator takes a reference to something. Think of it as like taking a pointer to a variable in C or C++. (Except that the memory-management is taken care of for you.) Therefore $rx points to whatever @xxx currently has. Updating @xxx is the same as updating @$rx, and vice versa.

If this bothers you, please describe what you'd expect from similar code with pointers in C or C++. Perl is just acting the same way.

If you want a private array you can use the anonymous array constructor, []. In the example that you gave, you'd get the result you were hoping for from:
\n@xxx = (1, 2, 3);\n$rx = [@xxx];\n@xxx = (9,8,7);\nprint($rx->[0] . "\\n");\n

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
     One line description of data model for Perl - (Arkadiy) - (33)
         Yep, you got it right. - (admin) - (1)
             You're not helping - (Arkadiy)
         Perl: Everything is... - (ChrisR)
         Over simplifying - (broomberg) - (1)
             linquistic versus data personalities - (tablizer)
         To pick up from what Barry said. - (static) - (4)
             No, everything is whatever Barry needs it to be - (Arkadiy) - (3)
                 The logic - (ben_tilly) - (2)
                     Keys... - (Simon_Jester) - (1)
                         Tie is NOT a module (and it sucks) - (ben_tilly)
         Well, Lisp - everything is a list or an atom... - (Simon_Jester) - (18)
             Atonm, list, hash... - (Arkadiy) - (17)
                 References are essentially pointers - (broomberg) - (7)
                     I am not trying to build anything in particular just now - (Arkadiy) - (6)
                         some answers - (cforde) - (5)
                             OK, another arbitrary distinction to remember - (Arkadiy) - (4)
                                 It's easy enough to test... - (Simon_Jester) - (2)
                                     Yes it is easy to test. - (Arkadiy) - (1)
                                         Testing has some disadvantages.... - (Simon_Jester)
                                 It isn't arbitrary - (ben_tilly)
                 References: pointers in languages that don't have pointers -NT - (FuManChu)
                 okay...look at it this way.... - (Simon_Jester)
                 PERL DOES NOT STORE LISTS!!! - (ben_tilly) - (6)
                     OK, in that case, what is (a,b,c) ? - (Arkadiy) - (5)
                         In which context? - (ben_tilly) - (4)
                             In the context of grammar and syntax - (Arkadiy) - (3)
                                 Simple answer: there is no syntactic difference - (ben_tilly) - (2)
                                     OK, I think I get it. - (Arkadiy) - (1)
                                         Yup, sounds like you've got it - (ben_tilly)
         lets try another viewpoint - (daemon)
         Sorry for not responding in this thread earlier - (ben_tilly) - (2)
             No worries. - (Arkadiy) - (1)
                 :-) -NT - (ben_tilly)

Loading times for MSN hosted pages are measured in tree-rings.
116 ms