in reply to How to Remove Commas ?

Dear allendevans,

Well that jumps out at me, since there are some very good coding habits for Perl also!

roboticus showed you the way you should write your code by scoping your variables with 'my' or 'local' or 'our'. If you had 'use strict' as roboticus had, your script would not compile and you would have the opportunity to write better Perl code. It is not that you pre-defined your variables, but that somehow that makes for better coding habits.

Do you think this code?

$date = ""; $time = ""; $ampm = ""; $filesize = 0; $filename = "";
is better or more clear than:
my ($date, $time, $ampm, $filesize, $filename) = split(" ", $line);
I like that you commented you code very well, but it was that comment above that needs to be changed/improved/removed. This may seem like a nit now, but if you continue to grow with Perl, then learning good Perl coding habits now, will help you in the future.

Good Luck!

"Well done is better than well said." - Benjamin Franklin

Replies are listed 'Best First'.
Re^2: How to Remove Commas ?
by allendevans (Initiate) on Mar 27, 2012 at 13:26 UTC

    flexvault,

    Thanks for taking the time to read my post and script. The comment is a quip for the professor, who made an off-hand comment during the lecture about declaring variables.

    Which is the better way of declaring variables ... that depends on what the compiler (or professor, or employer) wants! I do feel more at ease declaring my variables beforehand, though. legacy feelings of inadequacy left over from previous programming languages ... :)

    Thanks ... Allen.

      Were I your professor, I'd mark that quip down, since it's attached to a method of declaring variables that's legal, but falls far short of 'best practice.'

      Perl is not one of your "previous programming languages" and your response to your own rhetorical question about "the better way of declaring variables" isn't quite 'on-target' here. Perl (well, thru 5.10, IIRC) will accept the way you've done it, without even suggesting that there's a better way... which involves understanding scope... and the best practice of limiting a variable's scope as narrowly as possible.

      Best practice for declaring variables varies from programming language to programming language. But the declaration you used is very far from best practice in Perl.

      When declaring a variable you in Perl, it is best practice to declare whether it's a lexical (my) or package (our) variable. e.g.:

      my $counter = 0;

      It's also best practice to declare the variable in the tightest possible scope. For example, if a variable is used inside a loop, and needs reinitialising each time round the loop, then declare it inside the loop, not before the loop.

      Find the bug here:

      use 5.010; use strict; my $gender = 'unknown'; while (<DATA>) { chomp; given ($_) { when ("Alice") { $gender = 'female' } when ("Annie") { $gender = 'female' } when ("Andy") { $gender = 'male' } when ("Arnold") { $gender = 'male' } } say "$_ is $gender."; } __DATA__ Alice Andy Arnold Jennifer Annie Henry

      Output is:

      Alice is female.
      Andy is male.
      Arnold is male.
      Jennifer is male.
      Annie is female.
      Henry is female.
      

      Why is Jennifer male? Why is Henry female? It's because $gender is declared outside the loop, so is allowed to stay alive between loop iterations. Merely moving the one line where it's declared solves our subtle bug:

      use 5.010; use strict; while (<DATA>) { chomp; my $gender = 'unknown'; given ($_) { when ("Alice") { $gender = 'female' } when ("Annie") { $gender = 'female' } when ("Andy") { $gender = 'male' } when ("Arnold") { $gender = 'male' } } say "$_ is $gender."; } __DATA__ Alice Andy Arnold Jennifer Annie Henry
      Alice is female.
      Andy is male.
      Arnold is male.
      Jennifer is unknown.
      Annie is female.
      Henry is unknown.
      

      Perl variables have some pretty cool features, but they can trip you up. The precaution of adding use strict near the top of each script is generally a wise one. This one line tells Perl to force you to declare all your variables. This doesn't stop you shooting yourself in the foot, but it makes it difficult to shoot yourself in the foot accidentally. (You're still able to shoot yourself in the foot if you put in a bit of effort.)

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      allendevans,

      Glad it was from the professor!

      Have a good one!

      "Well done is better than well said." - Benjamin Franklin