Re: use strict; use warnings;

Discussion in 'Perl Misc' started by Justin C, Feb 24, 2014.

  1. Justin C

    Justin C Guest

    On 2014-02-24, Marek Novotny <> wrote:
    > Hi group,
    >
    > Tonight I finished up another obj for my class. The class has not yet
    > started to learn about use strict; and use warnings;


    Maybe they haven't taught you yet, but they are best practice, and
    will catch most errors, it saves the regulars here from pointing out
    the obvious.

    Use them, and then strip them out before your tutor sees your code.
    I've never heard of removing them breaking any code. [1]


    > I was given the following to solve:


    [snipped]

    Have they taught you about random numbers yet? I can't see how it's
    possible to shuffle without random numbers.

    However, you've been told to use pop, shift and push. Maybe you could
    create a new array, pushing cards into it that you've 'pop'ped and
    'shift'ed from the old array in some pseudo random fashion? [2]

    [snipped]

    > 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.


    @diamonds is the whole array, $diamonds[2] refers an array element.

    (the same works for hashes, but you probably haven't done those yet:
    %coffee is the whole hash, but $coffee{espresso} refers to a portion
    of data *in* the %coffee hash).

    The @ and % refer to the whole. $ and [suffix] or {suffix} refer to
    a part for arrays and hashes respectively.


    > I tried to make the code as simple to understand as possible.
    >
    > Anyone care to take a stab at what it would have to look like with the
    > use of strict and warnings so I can understand what I should be doing?


    That's a lot of work for grabbing the wrong end of the stick. :(

    Justin.

    1. except:
    no warnings 'uninitialized';
    # and similar
    but you won't be using that without having been taught about 'use
    warnings' anyway).

    2. Maybe something like:
    for (my $i = 0; $i < $size_of_array; $i++) {
    if ($i % 3) {
    push @new, shift @array;
    }
    else {
    push @new, pop @array;
    }
    }
    You might want to wrap all of that in another loop and run it a
    few times. It should give *the appearance* of a shuffled deck,
    but you can see it'll shuffle identically each time, so it's not
    a real shuffle - if running it more than once be sure to replace
    @array with @new each time or you'll shuffle the first array
    each time, and not the partly shuffled one!

    --
    Justin C, by the sea.
     
    Justin C, Feb 24, 2014
    #1
    1. Advertisements

  2. Justin C

    $Bill Guest

    On 2/24/2014 02:21, Justin C wrote:
    >
    > Have they taught you about random numbers yet? I can't see how it's
    > possible to shuffle without random numbers.


    'Shuffling' and 'random shuffling' are two different things.

    You're 'assuming' he is wanting a random shuffle.
     
    $Bill, Feb 25, 2014
    #2
    1. Advertisements

  3. Justin C

    Justin C Guest

    On 2014-02-24, Ben Morrow <> wrote:
    >
    > Later on you will meet globs, which have a * sigil; again, this is not
    > related in any way to the use of * to mean multiplication.



    I think I've only ever used globs when doing the exercises in
    Learning Perl.


    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Feb 25, 2014
    #3
    1. Advertisements

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. Robin

    strict and warnings

    Robin, Jan 21, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    205
    Jonathan Stowe
    Jan 21, 2004
  2. Ted Sung
    Replies:
    1
    Views:
    549
    Sherm Pendley
    Aug 30, 2004
  3. Marius Gavrilescu

    Re: use strict; use warnings;

    Marius Gavrilescu, Feb 24, 2014, in forum: Perl Misc
    Replies:
    152
    Views:
    1,177
    $Bill
    Mar 11, 2014
  4. Mart van de Wege

    Re: use strict; use warnings;

    Mart van de Wege, Feb 24, 2014, in forum: Perl Misc
    Replies:
    1
    Views:
    166
    Mart van de Wege
    Feb 25, 2014
  5. Janek Schleicher

    Re: use strict; use warnings;

    Janek Schleicher, Feb 24, 2014, in forum: Perl Misc
    Replies:
    4
    Views:
    235
    $Bill
    Feb 25, 2014
  6. Jens Thoms Toerring

    Re: use strict; use warnings;

    Jens Thoms Toerring, Feb 24, 2014, in forum: Perl Misc
    Replies:
    2
    Views:
    273
    Jens Thoms Toerring
    Feb 25, 2014
  7. John Bokma

    Re: use strict; use warnings;

    John Bokma, Feb 24, 2014, in forum: Perl Misc
    Replies:
    1
    Views:
    167
    John Bokma
    Feb 24, 2014
  8. Rainer Weikusat

    Re: use strict; use warnings;

    Rainer Weikusat, Feb 27, 2014, in forum: Perl Misc
    Replies:
    1
    Views:
    231
    Tim McDaniel
    Mar 1, 2014
Loading...