Re: use strict; use warnings;

Discussion in 'Perl Misc' started by Janek Schleicher, Feb 24, 2014.

  1. Am 24.02.2014 06:31, schrieb Marek Novotny:
    > ...
    > @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
    > "9 H","10 H","J H","Q H","K H",
    > "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
    > "9 D","10 D","J D","Q D","K D",
    > "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
    > "9 C","10 C","J C","Q C","K C",
    > "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
    > "9 S","10 S","J S","Q S","K S");
    > ...


    Beside your problem you asked,
    one important lesson in programming is to avoid to reapeat yourself,
    as we are lazy :) and more important it would be heart to maintain
    (change or update your program in future).
    Just imagine here, you might also play with a double deck or with jokers
    or without deuces (or you'd prefer to have an other starting deck order
    of cards), than with this way, you would have to copy+paste and replace
    it manually with a lot of keystrokes. Not only is this boring, more
    important, it is easy to make a mistake doing so.

    Here a nice and simple way that also fits into your course (working with
    arrays and loops would be) [untested]:

    my @startingdeck = ();
    foreach my $rang (2 .. 9, qw/T J Q K A/) {
    foreach my $color(qw/H D C S/) {
    push @startingdeck, "$rang $color";
    }
    }
    # Starting deck is now filled with a classic 52 card deck

    So it's easy to read, short to write and easy to change,
    and most important you Don't Repeat Yourself (DRY-principle).


    Greetings,
    Janek

    PS: You problably don't know yet what this qw/../ operator means,
    here it's just a short cut for
    qw/H D C S/ is the same as ("H", "D", "C", "S").
    Janek Schleicher, Feb 24, 2014
    #1
    1. Advertising

  2. Janek Schleicher

    John Black Guest

    In article <>, says...
    > my @startingdeck = ();
    > foreach my $rang (2 .. 9, qw/T J Q K A/) {
    > foreach my $color(qw/H D C S/) {
    > push @startingdeck, "$rang $color";
    > }
    > }
    > # Starting deck is now filled with a classic 52 card deck


    Nice!

    John Black
    John Black, Feb 24, 2014
    #2
    1. Advertising

  3. Am 24.02.2014 18:50, schrieb Marek Novotny:
    > That they did cover. For whatever reason I like @array = qw( elem1
    > elem2 ...) but I see everyone here has adopted qw/elem1 elem2/. I could
    > easily adapt to this instead. For whatever reason the first is easier for
    > to remember and think of.


    Both are usual but qw/.../ is more common.

    Later you'll learn also many operators looking similiar
    q/.../ same as '...'
    qq/.../ same as "..." (both practical if there a lot of ' or " inside)
    /.../ just for a regexp [here you can't use anything else than a /]
    qr/.../ also for a regexp that can be reused
    s/.../.../ for substion(s)
    tr/.../.../ for translaterating [like replace all A's with Z's]
    y/.../.../ as synonym for that
    qx/.../ for executing the command inside at the shell or system
    and probably some I forgot also.

    So, most use just /.../ as we Perler are used to it and it tells us on
    first glance, inside those /.../ is a kind of string that gets operated on.

    When seeing something like (...) or {...}, we expect on first glance
    that there is code to execute instead.

    Indeed you can use allmost most all characters as seperators for all
    those operators,
    so using something like
    q!...! or q#...# (e.g. if there a lot of '," and / inside the strings)
    qw{... ...} (if there are a lot of / and ( ) inside)
    is possible also.

    There's more than one way to do it, but for the beginning,
    just use qw/.../ or qw( ... ) whatever you like more and feel
    comfortable with both if you see it in foreign source code.


    Greetings,
    Janek

    PS: BTW, if you see a // that can mean a lot of other stuffs, too, and
    without much context you couldn't even be sure :D, but atm that doesn't
    matter for you.

    PPS: There is a reason why can make most obfuscating Perl program
    contests :))
    Janek Schleicher, Feb 24, 2014
    #3
  4. Janek Schleicher

    John Bokma Guest

    Janek Schleicher <> writes:

    > Am 24.02.2014 18:50, schrieb Marek Novotny:
    >> That they did cover. For whatever reason I like @array = qw( elem1
    >> elem2 ...) but I see everyone here has adopted qw/elem1 elem2/. I could
    >> easily adapt to this instead. For whatever reason the first is easier for
    >> to remember and think of.

    >
    > Both are usual but qw/.../ is more common.


    Not sure about that one, but:

    > So, most use just /.../ as we Perler are used to it and it tells us on
    > first glance, inside those /.../ is a kind of string that gets
    > operated on.


    That's a good motivation to pick // over (), thanks.

    --
    John Bokma j3b

    Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
    Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
    John Bokma, Feb 24, 2014
    #4
  5. Janek Schleicher

    $Bill Guest

    On 2/24/2014 15:55, Ben Morrow wrote:
    >
    > Personally I use qw() when the construction will extend over several
    > lines; it makes the close-quote look better. I also tend to use q{} for
    > (and only for) quoting Perl, because, as you say, a {} block looks like
    > it should have code inside.


    I always use qw(...) over qw/.../ it is a list after all and don't see
    what /.../ gets you that would equate to a list/array.

    There are only maybe 10 examples in the entire Perl man pages that use
    qw// vs qw() which has hundreds of examples.
    $Bill, Feb 25, 2014
    #5
    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:
    450
    $Bill
    Mar 11, 2014
  2. Kaz Kylheku

    Re: use strict; use warnings;

    Kaz Kylheku, Feb 24, 2014, in forum: Perl Misc
    Replies:
    0
    Views:
    57
    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:
    46
    George Mpouras
    Feb 24, 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:
    46
    Mart van de Wege
    Feb 25, 2014
  5. Justin C

    Re: use strict; use warnings;

    Justin C, Feb 24, 2014, in forum: Perl Misc
    Replies:
    2
    Views:
    69
    Justin C
    Feb 25, 2014
Loading...

Share This Page