in reply to Re: Special formatting of error messages
in thread Special formatting of error messages

This works perfectly thanks. But....it leads me to a small additional question. Map works on a list and sets up the value of maxsize. This looks like a regex (horror of horrors!) I'm not too familiar with anything but simple regexs! Sorry to be a pest but can you confirm my understanding of this. Assuming map works on a list, the code on the right hand of the ? uses the length function on the row being processed and if it's greater than $maxsize (which was initialised with a value of zero) then maxsize is updated with this new value ( I think). The bit that's confusing me is after the ? What is the purpose of the ($maxsize):(length)? It can't be returning the value of length into $maxsize as that would render the first > test as pointless, wouldn't it?
Cheers, Thick Ronnie
  • Comment on Re^2: Special formatting of error messages

Replies are listed 'Best First'.
Re^3: Special formatting of error messages
by osunderdog (Deacon) on Feb 02, 2005 at 13:51 UTC

    Let me break this down:

    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.

      Thanks for the explanation, you correctly identified the area I was having problems with and have explained it fully.
      Cheers,
      Ronnie
      PS I just need to find out how to centre the text now.

        Bah! Looks like printf won't do centering. Have to calculate the space by hand. (As another responder posted...)

        use strict; my @lines = <DATA>; my $maxsize = 0; map {chomp; $maxsize = ($maxsize>length)?($maxsize):(length)} @lines; print "max size: $maxsize\n"; my $windowSize = $maxsize +4; my $bar = '*' x $windowSize . "\n"; foreach (@lines) { # calculate space between this line and window border my $space = $maxsize - length($_); # determine if the space is an odd or even number my $odd = $space%2; # half the space will go on the left, half will # go on the left (plus one if it was odd). my $spacer = int($space/2); print($bar); print('* ' . ' ' x $spacer . $_ . ' ' x ($spacer + $odd) . " *\n"); print($bar); } __DATA__ some lines that are of different sizes Some small really small. ----------------some large--------------------- --------------------------------------------------some huge----------- +--------------------------------------- ...

        "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.

      The shortest I've got so far is my $max = (sort map { length } @test)[0]; This can also get the shortest by using -1 instead of 0.


      ___________
      Eric Hodges

        Bravo! I hadn't thought about doing it with sort.

        I came up with:

        use List::Util qw|max|; ... my $maxsize = max(map {chomp; length} @lines);

        "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.