codes
Sunday, April 23rd, 2006Would a person’s mental state be determined by the way he codes?
My code:
# to use: cat file|perl prog.pl
#!/usr/bin/perlmy $chk;
my $text;
my $name;
my $line;$chk=0;
while (<>) {
chomp;if($chk==1)
{
$text.="$_\n"; }
if (/^$/)
{$chk=1;}else
{$chk=0;}
}for(split /\n/, $text){
my ($first, $second) = split;
$name .= $first ."\n";
$line .= $second ."\n";
}#print $text;
print $name, "\n";
print $line;
I thought my code was neat, till…
His code:
#!/usr/bin/perlmy %tbl;
local $/ = undef;
my $all = <>;for ($all =~ /(.*?\n{2,}|.+)/sg) {
my ($first, $second) = split;
push @{$tbl{$second}}, $first;
}map {
print "$_: ";
map {
print "$_, ";
} @{$tbl{$_}};
} (keys %tbl);