Created
February 26, 2013 20:46
-
-
Save tryperl/5042013 to your computer and use it in GitHub Desktop.
Created by www.tryperl.com.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Created by tryperl.com | |
## This is a demo of a perl script from a stackoverflow answer here : http://stackoverflow.com/a/14818869/368070 | |
# I thought of that answer as a benchmark when designing some of this app :) | |
#Paste this in the STDIN tab. | |
#precedence = 2 | |
#new york | |
#new jersey | |
#florida | |
#precedence = 3 | |
#kings | |
#essex | |
#dade | |
#precedence = 1 | |
#brooklyn | |
#newark | |
#miami | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
my $hash = 1; | |
if($hash) { | |
###################### This is for Hash of H | |
my $precedence = 0; | |
my %data; | |
while (<>) { | |
chomp; | |
if (/precedence\s*=\s*([0-9]+)\z/) { | |
$precedence = $1; | |
next; | |
} | |
push @{ $data{$precedence} }, $_; | |
} | |
print Dumper(%data); | |
my @final = map @{ $data{$_} }, sort { $a <=> $b } keys %data; | |
print Dumper(@final); | |
} else { | |
###################### This is for AoA | |
my $precedence = 0; | |
my @data; | |
while (<>) { | |
chomp; | |
if (/precedence\s*=\s*([0-9]+)\z/) { | |
$precedence = $1; | |
next; | |
} | |
push @{ $data[$precedence] }, $_; | |
} | |
my @final = map @$_, grep $_, @data; | |
print Dumper(@final); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment