codes

Would a person’s mental state be determined by the way he codes?

My code:
# to use: cat file|perl prog.pl
#!/usr/bin/perl

my $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/perl

my %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);

Leave a Reply