in reply to Selective Printing of Lines

Had a beer at lunch and don't feel like doing my own work...

use strict ; use warnings ; my @data = sort <DATA> ; my %min ; foreach (@data) { my ($col1, $col2, $col3) = split /\s+/, $_ ; $min{$col2} = $col1 if (! defined $min{$col2}) ; print $_ if ($col1 eq $min{$col2}) ; } __DATA__ -1 2 ereu 1 2 rerer 2 2 rere 3 2 eee 3 2 wwwe 9 2 qqewew 9 2 vvvgf -1 2 wwwew 4 3 wwew -9 3 wwss 6 3 jjuu 7 3 kkii 7 3 ggtyu 8 3 ffder 1 4 rrtrr 1 4 rree

Replies are listed 'Best First'.
Re^2: Selective Printing of Lines
by polsum (Novice) on Sep 16, 2011 at 00:24 UTC
    THank you all for excellent replies. All of them are working fine and now I am trying to 'decode' each of them :D. Hi onelesd, Can you please explain the if (! defined $min{$col2}) part of your code. Ever since you posted this code, I have been banging my head to understand the usage of defined and the unary operator !. Thanks in advance.

      See: perldoc -f defined

      defined EXPR defined Returns a Boolean value telling whether EXPR has a valu +e other than the undefined value "undef".

      ! negates the condition, so in natural language the condition is: "if $min{$col2} is not defined".