in reply to Re: Is this bad coding style?
in thread Is this bad coding style?

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');

Replies are listed 'Best First'.
Re^3: Is this bad coding style?
by pemungkah (Priest) on Jan 10, 2009 at 00:58 UTC
    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. :)
Re^3: Is this bad coding style?
by leocharre (Priest) on Jan 30, 2009 at 20:11 UTC

    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.