my $maxsize = 0;
map {chomp; $maxsize = ($maxsize>length)?($maxsize):(length)} @lines;
If I were to write this using the 'Software Engineer' side of my brain, it would look like:
my $maxsize = 0;
foreach my $line (@lines)
{
chomp $line; #remove \n from the line
my $lineLength = length($line); #Determine length of line
#if line is longer than the longest
#line I've seen so far then make it $maxsize.
if($lineLength>$maxsize)
{
$maxsize = $lineLength;
}
}
code hasn't been tested
It sounds like the part that is throwing you is the '?:' conditional operator:
$result = ($x > $y)?(14):(-5)
It's a one line conditional. It's documented in perldoc perlop (look for 'Conditional Operator'). The condition is evaluated and if true, the first value is assigned into $result (14) if it's false, the second value is assigned into $result (-5).
I hadn't done it this way before and I thought your question was a good opportunity to try it out. I've seen it before in code, just haven't had a chance to see if it suits me.
To any monks that have read this far. What's the minimal code for finding the lonest line in an array?
"Look, Shiny Things!" is not a better business strategy than compatibility and reuse.
|