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

Hi Im new here. Just wasnt sure were to post compared to cplusplus but Im having a problem with an array print out. On line 33 when I print out wrongPasswords, there is a space in front of my array. Just wondering why. Thanks

#Guess Password Program for Perl $password = "perlclass"; print"What is the password to access the system\n"; $count = 0; @wrongPasswords[4] = (); while($guess ne $password && $count != 4 ){ $guess = <stdin>; chomp($guess); if($guess eq $password){ print"You have unlocked the program\n"; }#ends if statement else{ push(@wrongPasswords,$guess); if($count != 3){ print"Sorry wrong choice! Try again\n"; }#ends if else{ print"\nYou have enter too many passwords!\nProgram Locked!!\n"; print"Here are the passwords that were entered\n"; print"@wrongPasswords\n"; }#ends 2nd else }#ends 1st else $count++; };#ends while loop system("pause");

Replies are listed 'Best First'.
Re: Password Program
by syphilis (Archbishop) on Mar 01, 2015 at 00:37 UTC
    If, prior to printing out @wrongPasswords, you check the number of elements in the array (print scalar @wrongPasswords) you will find there are 9 - the 5 empty ones that you originally created, plus the 4 wrong guesses that were subsequently pushed onto the array.

    Basically, instead of:
    @wrongPasswords[4] = ();
    you want:
    @wrongPasswords = ();

    Cheers,
    Rob

      That is so strange I couldve sworn I did that before. But your right!! Thank you so much syphilis.

Re: Password Program
by AnomalousMonk (Archbishop) on Mar 01, 2015 at 00:23 UTC

    What is the effect of the statement
        @wrongPasswords[4] = ();
    on the array? If the array is dumped immediately after this statement (see Data::Dump::dd), what is its content? Why? (If this statement is run with warnings asserted, what will Perl say about the statement?)

    Does the push built-in have any effect on elements already in an array to which a new element is push-ed?

    If a Data::Dump::dd statement for the  @wrongPasswords array is inserted immediately before the
        print"@wrongPasswords\n";
    statement, what is the content of the array, and why?

    Update: This is a very belated afterthought, but it occurs to me that because programmercarlito is clearly a novice and I have no idea of his or her ability to install modules, I should rather have used Data::Dumper, which is core, in the example and not Data::Dump, which is not core.

    So the use statement could be
        use Data::Dumper;
    and the invocations would be
        print Dumper \@wrongPasswords;
    rather than
        dd \@wrongPasswords;


    Give a man a fish:  <%-(-(-(-<

      If this statement is run with warnings asserted, what will Perl say about the statement?

      Unfortunately, nothing... the "Scalar value @wrongPasswords[4] better written as $wrongPasswords[4]" warning you're probably referring to doesn't seem to get triggered by the assignment.

      $ perl -wMstrict my @foo; @foo[3] = "bar"; print @foo[3], "\n"; __END__ Scalar value @foo[3] better written as $foo[3] at - line 3. bar
        Well, yes, it seems to be trigerred by the assignment:
        $ perl -wMstrict -e 'my @foo; @foo[3] = "bar";' Scalar value @foo[3] better written as $foo[3] at -e line 2.

        Je suis Charlie.
Re: Password Program
by Anonymous Monk on Mar 01, 2015 at 00:26 UTC

    Welcome! Here's how you can figure out this problem - and many others! - yourself:

    Once you've done that, have a look at @wrongPasswords with Data::Dumper, as described in the Basic debugging checklist item 4. You will see that it looks something like this:

    $VAR1 = [undef, undef, undef, undef, undef, "one", "two", "three", "four"];

    What is going on here is that the line @wrongPasswords[4] = (); isn't quite right. You're probably trying to say "initialize the array @wrongPasswords to a size of four", but that's not necessary as Perl has dynamically sized arrays (kind of like a vector in C++). Instead, in Perl, @wrongPasswords[4] is what is known as an array slice, and you're telling Perl you want to write an empty list to the index 4 of the array, so to do that, Perl automatically sizes your array to five elements and initializes them to undef (a little bit like NULL). In your loop you use push, which adds elements to the end of the array. So when you say print "@wrongPasswords\n";, what you're seeing is five empty strings (the undefs) separated by spaces, followed by the rest of the items of the array. In this case, to set up @wrongPasswords, all you need to do is write my @wrongPasswords;.