Re: use strict; use warnings;

Discussion in 'Perl Misc' started by Mart van de Wege, Feb 24, 2014.

  1. Marek Novotny <> writes:

    >
    > When I started to try to use the use strict and warnings, I got warnings
    > such as: Scalar value @diamonds[$i] better written as $diamonds[$i] at ./
    > obj10.pl line 55.
    >
    > But the whole point of what I am doing is loading elements into an
    > array... So I wanted it as @diamonds, not $diamonds, and so on.
    >

    Yeah, this is a common mistake.

    Single elements of an @array are scalars themselves, so they are
    addressed as $elem[$index]. The @array[$number] syntax is used to take a
    *slice* of an array. This might work (it usually does), but can cause
    hard to find bugs.

    Mart
     
    Mart van de Wege, Feb 24, 2014
    #1
    1. Advertising

  2. Marek Novotny <> writes:

    > On Mon, 24 Feb 2014 08:31:47 +0100, Mart van de Wege wrote:
    >> Single elements of an @array are scalars themselves, so they are
    >> addressed as $elem[$index]. The @array[$number] syntax is used to take a
    >> *slice* of an array. This might work (it usually does), but can cause
    >> hard to find bugs.
    >>
    >> Mart

    >
    > It's really good to see this. I'm actually glad I ran into this. I'll
    > remember this lesson for sure.
    >

    It's a hard rule in Perl: array elements are *always* scalars (single
    values). Even if they appear to be not (then they are usually
    references).

    Keep that rule in mind, it will make things easier once you start on
    references.

    Mart
     
    Mart van de Wege, Feb 25, 2014
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marius Gavrilescu

    Re: use strict; use warnings;

    Marius Gavrilescu, Feb 24, 2014, in forum: Perl Misc
    Replies:
    152
    Views:
    515
    $Bill
    Mar 11, 2014
  2. Kaz Kylheku

    Re: use strict; use warnings;

    Kaz Kylheku, Feb 24, 2014, in forum: Perl Misc
    Replies:
    0
    Views:
    68
    Kaz Kylheku
    Feb 24, 2014
  3. George Mpouras

    Re: use strict; use warnings;

    George Mpouras, Feb 24, 2014, in forum: Perl Misc
    Replies:
    0
    Views:
    58
    George Mpouras
    Feb 24, 2014
  4. Justin C

    Re: use strict; use warnings;

    Justin C, Feb 24, 2014, in forum: Perl Misc
    Replies:
    2
    Views:
    80
    Justin C
    Feb 25, 2014
  5. Janek Schleicher

    Re: use strict; use warnings;

    Janek Schleicher, Feb 24, 2014, in forum: Perl Misc
    Replies:
    4
    Views:
    87
    $Bill
    Feb 25, 2014
Loading...

Share This Page