peacefuldragon has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, perl novice here. I've been using perl for under a week, our class has decided to do a 2-week crash course into the wonderful world of perl to complete our year. As such we were given a simple task (or so it seemed). Ask the user to enter 16 digits, then print those 16 digits out in 4 groups of 4 (IE: 1234 1234 1234 1234). So far I have managed to do this:
use strict; use warnings; print 'Please enter your card number' chomp($inp = <>); $inp =~ s/(\d{4})(\d{4})(\d{4})(\d{4})/$1 $2 $3 $4/; print $inp,"\n";
It didn't work and I want to see what I've done wrong and what I need to fix in order to reach my end goal. Thank You

Replies are listed 'Best First'.
Re: Passing digits in perl
by ww (Archbishop) on Apr 27, 2015 at 02:35 UTC

    You have two fine answers to your syntax question.

    But it's also time, as a "perl novice" to think about the clarity and precision of your spec and your prompt. Who's to know from what you provided that numbers cannot be separated by spaces nor can they be comprised of multiple digits? (Well, who'd know at a glance from the spec that the numbers can't be made up of multiple digits; the code makes that implicit but you can't count on users catching that immediately.) So, make your prompt clear:

    print 'Please enter 16 numbers (no spaces, no multi-digit numbers): ';

    Note also the addition of a colon and a space: that's not really necessay, but, IMO, doing so gives the user a better hint that it's time to start entering the numbers.

    And, critically, as you move along, think "precision and clarity" when you when you ask a question and when you develop a spec. You'll really appreciate the results if you have to go back to something you've written about a potentially ambiguous task.

    Afterthought: AnonyMonk (the writer of Re: Passing digits in perl) may have a valid if cryptic point: your code assumes the user will correctly input 16 numbers but a miscount or a typo will fail without explanation. Try the suggestion to enter just 5 numbers or enter 16 numbers using hex digits, say "1234567890ABCDEF." That satisfies the spec of 16 numbers but won't work with your regex.

    Moral of this digression: Test the input; if it doesn't match exactly 16 Arabic digits, tell the user; or make the (already too-long) prompt a bit longer by specifying what kind of numbers are acceptable. Alternately, and as a challenge for you and your class-mates, modify the code to ACCEPT hex digits ... and to REJECT inputs where the user enters more than 16 numbers.

Re: Passing digits in perl
by marinersk (Priest) on Apr 26, 2015 at 23:58 UTC

    The error, translated to newbie terms, is that you didn't define $inp
    You define your variables using mymost of the time.

    Adding myreveals a slightly less obvious error -- you neglected to end your first printstatement with semi-colon. :-)

    Because you wisely did use strict;, it gave the following errors:

    D:\PerlMonks>digits1.pl syntax error at D:\PerlMonks\digits1.pl line 5, near "chomp" Global symbol "$inp" requires explicit package name at D:\PerlMonks\di +gits1.pl line 5. Global symbol "$inp" requires explicit package name at D:\PerlMonks\di +gits1.pl line 6. Global symbol "$inp" requires explicit package name at D:\PerlMonks\di +gits1.pl line 7. Execution of D:\PerlMonks\digits1.pl aborted due to compilation errors +. D:\PerlMonks>

    Fixed:

    use strict; use warnings; print 'Please enter your card number'; chomp(my $inp = <>); $inp =~ s/(\d{4})(\d{4})(\d{4})(\d{4})/$1 $2 $3 $4/; print $inp,"\n";

Re: Passing digits in perl
by Anonymous Monk on Apr 27, 2015 at 01:08 UTC
Re: Passing digits in perl
by Anonymous Monk on Apr 26, 2015 at 23:58 UTC
    what did you type in? Try  $inp = '12345'; and see what happens