in reply to Re: Useless use of private variable in void context
in thread Useless use of private variable in void context

Using for(my $i=0; $i<$Size; $i++) is wrong here, because it means that $i will be 0 after the loop, while another part later on requires it to be scalar @Participants.

Note that there's a much more perlish way to write the whole loop:

for my $p (@Participants) { if ($p eq $UserID) { return 1; } }

If the user ID was not found, $i will always be the array size. Which means that you can replace $Participants[$i]=$UserID; with push @Participants, $UserID;, which is a bit more readable.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^3: Useless use of private variable in void context
by okarmi (Initiate) on Oct 12, 2009 at 10:57 UTC
    Dear Moritz,

    First of all thank you for your help.

    1. There's something that I'm not sure I understand, you say that when I use for ($i=0; $i<$size; $i++), after the loop ran a few times and than ended ($i was incremented a few times) it is set back to 0 once the loops ends? (You don't really have to answer that cause I'm testing it later today).

    2. Your perlish way of writing the loop is interesting. I only know a bit of C (one university class) and that is why I wrote the loop the way I did. Do you advice me to start writing in your format or should I stick to my old C style?

    Thank you so much for contributing your wisdom to a novice like me.

    okarmi

      Only if you use for (my $i; ... ) the variable has the old value again.

      $i = 3; for (my $i = 0; $i < 30; $i++) { # \ # do something here # | scope of inner $i variable } # / print $i, "\n"; # prints 3 because it's the outer variable again

      Regarding your second question, that's really a matter of preference and learning. If you plan to do more with perl, it will pay off to learn common Perl idioms, and use them.

      If you have access to a public library, I'd very much recommend the book Programming Perl by Wall, Christiansen and Orwant

      Perl 6 - links to (nearly) everything that is Perl 6.

        Thank you again for your wisdom and patience!

        okarmi

      Welcome to the Monastery.

      It depends how fluent you wish to be - how much you wish to sound like a "native".

      The c-style loop you used is fine, it gets the job done, but it is C written in perl. It would be similar to my trying to speak Spanish to a native speaker. I could convey meaning (hopefully the correct meaning), but would in no way be considered fluent. In some cases (transferring English idioms into Spanish, for example), I would only be understood by someone that could follow how I got to the translation I used.

      If I were to immerse myself in the language, I would hope that I would become more fluent, and stop trying to use English idioms in was that do not make sense. I might even start using Spanish idioms effectively.

      It comes down to communicating ideas. As you start to use the idioms and base your communication on the language at hand, you can be more effective at transferring your ideas to others.

      Many times when one comes into Perl from another language, that person will write their other language in Perl. It isn't wrong to use the c-style loop, there are just more perlish ways to do it.

      I am also not saying that you should never use the c-style loop. There are times when it would be more appropriate than the for/foreach loop above.

      --MidLifeXis

      Perl for loops are more compact, easier to understand and safer than a C for loop. All these arise from the Perl for loop having fewer moving parts:

      for my $element (@elements)

      compared with the equivalent C loop header:

      for (my $index = 0; $index <= @elements; ++index) { my $element = $elements[$index];

      If what you want to do in the loop is use $element (and in the vast number of situations it is) then the Perl loop 'wins' by a country mile.

      The biggest problem with C style loop is ensuring that you don't introduce off by 1 errors. It's easy to do and can be hard to spot.


      True laziness is hard work