in reply to Is this bad coding style?

Bad coding style is a little vague..

You could be meaning syntax- which is more about formatting- or maybe symbol names (variable names, etc) - or your lack of use strict, or your use of foreach instead of for.. etc etc.

Here's how I would naturally code this, myself..

my @a = qw/1 2 3 # 6 7 # 9 10/; for my $val ( @a ){ $val eq '#' ? do_something() : $val eq '1' ? do_where_var_is_1() : do_something_else(); }

Why am I naming a variable $val instead of simply using $_ ? Because it's more intuitive when I look at it- to mean that I am checking that value, if I were using the value, I may name it $arg.

Why use a for instead of foreach? I use foreach with hashes, for with arrays, something personal.

Why use the ternary operator instead of if and else? For brevity and because it rocks.

Why do I use eq for both 1 and # ? Because it makes the code make more sense for human eyes. ( Besides you don't need to differentiate between numbers and strings here- they are all strings, right? That is, it appears the sample data you may have is by default all string- and it may be a character number or something else. You can't have all numbers and one character and still have all numbers, but you can have all c... anyways.. )

That's what *I* would do naturally. But everyone has a style. What's important is that is work, and it's also important that another coder can look at it and easily tell what you are doing.

Replies are listed 'Best First'.
Re^2: Is this bad coding style?
by Anonymous Monk on Jan 08, 2009 at 17:30 UTC

    Thanks, leocharre.

    What if @array was populated like this:
    my @array = (1 .. 9); # real numbers push(@array, '#');
    Does it still make sense to use "eq" and compare each of the elements of @array with '1' ? ie. if ($var eq '1');
      Yes, that's fine, but. It would be more useful to ask "will this give me the output I want" (with a sample output provided) rather than "is this code OK". This is the equivalent of saying "perl -c reports no syntax errors - guess it works!" instead of "my tests pass - guess it works!".

      1. Do you want to use the # as a delimiter in the array?
      2. Do you want to throw out any '#' entries?
      3. Is there some other result that I haven't guessed?
      To make sub-arrays at each '#':
      use strict; use warnings; use Data::Dumper; my @broken_up; my @array = qw( 1 2 3 # 4 5 # 6 7 8 9 # 10); my $position = 0; $broken_up[0] = []; foreach my $item (@array) { if ($item eq '#') { $position++; $broken_up[$position] = []; } else { push @{ $broken_up[$position] }, $item; } } print Dumper(\@broken_up);
      gives
      $VAR1 = [ [ '1', '2', '3' ], [ '4', '5' ], [ '6', '7', '8', '9' ], [ '10' ] ];
      Now you have an array of arrays, broken at the '#'s. (The extra level of nesting is because I dumped a reference to the output array.)

      If you just want to throw the '#'s out:

      @array = qw( 1 2 # 3 4 5 # 6 7 8 9 # 10); @array = grep { $_ ne '#' } @array; print "@array\n";
      gives
      1 2 3 4 5 6 7 8 9 10
      Notice that these are wildly different, because we don't know what you're actually trying to do. Obviously it doesn't matter whether or not the style is good ... if the result is wrong. :)

      Well, would you know that these are all numbers? Or that they are not all numbers?

      Part of the strength (or weakness) of perl- is that it's not strongly typed. That means, you can define a symbol ( create a variable name like 'my $a;') before you say what the heck it is supposed to hold.. and .. well.. there's some more to it.. basically you let perl worry about it.

      Look up strong vs weak typed languages on google.