Simple perl question? Simple advice please

Discussion in 'Scripting & Programming' started by dee1810, Oct 2, 2007.

  1. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi All

    I know that this is too simple to warrant your time, :oops:, but the theory that I have just studied tells me that its contradiction. (CIW perl Fundamentals)

    Here goes:

    @array = (3,6,5,4,1);
    @array = sort {$a <=> $b} @array;
    $array[0] = 2;
    print ("@array");

    the answer that my perl interpreter gives me is:
    2 3 4 5 6

    My understanding is:
    1st Line - Here is your array detailing your elements.
    2nd line - Now sort these elements in ascending order :rolleyes: Yes.. I'm still following
    3rd line - the scalar variable in position 0 is 2 :x No, I don't see it!!!! Position 0 is 1, not 2.
    I understand the theory, but can't work out why the 2 is included in the array when there was no mention of either a push or unshift function????
    Silly, I know.

    Please explain.
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  2. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    admittedly I dont know Perl, but if it acts anything like most languages line 3 is actually setting the first item in the array to 2.

    On initialisation, your array is (3,6,5,4,1)
    After sorting, its (1,3,4,5,6)
    Line three sets item(0) to 2, so the array is now: (2,3,4,5,6)

    EDIT: Missed the section which confirms that this is the correct interpretation.

    Remeber, this:
    Code:
    $array[0] = 2;
    Is not a true/false query, its an action. Its not asking if it is equal to 2, its telling the array to set item 0 to 2.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  3. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi Fergal.
    Thanks for this...

    I wonder why this number 9 is ignored then? Perhaps because it isn't being sorted.
    @array1=(10..13);
    @array2=@array1;
    $array1[0]=9;
    print ("@array2");

    the answer is 10 11 12 13

    :eek:
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  4. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Sorry mate... Just noticed your edit.....

    Thanks again.
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  5. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Because you changed array1, but printed array2. They are entirely separate arrays.

    If you want array2 to track array1 then you need references.


    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  6. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    its not being ignored. you aren't printing array1 out.

    When you set array2 to equal array1, you are actually creating a copy of the array object. After that point, the two items become separate entities, and changes to one do not affect the other.

    What you need to remember, is that there are two types in programming: Value Types and Reference Types. How they behave is different.

    What I believe is the case here, is that the array of integers is actually a Value Type (Certainly when I test the type of an integer array it returns as system.int32 in vb.net). When you set array2 to equal array1, because its a value type, the code actually creates a copy of array1 for array2. From that point on, the two items are totally separate, and any changes to one have no effect on another.

    If the array were a collection of objects, then it would be a reference type, in which case setting array2 to equal array1 would copy the reference pointer, rather than the object itself. Both variables would point to the same place in memory and so, and changes to one would affect the other.

    Does that make sense?
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  7. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    Its actualy array 2 that is being printed
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  8. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    thats what i said, aren't
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  9. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    Hey dee, long time no see. Im revising for my Perl exam and you are just infront of me. Maybe we can study together. PM me if that sounds cool.
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  10. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76

    Sorry mate, my minds being scrambled by regular expressions.
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  11. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Thanks Guys.. I understand a little better now.
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  12. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    No problem. :biggrin I hate RegEx's. Luckily ive managed to avoid them so far.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  13. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Once you get the hang of Regexes there are amazing things you can do with it, which are much harder to do any other way.

    It is a shame a complex Regex tends to be Write-only though! :biggrin

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.