Skip to content

Instantly share code, notes, and snippets.

@Util
Forked from Woody2143/Schwartzian.pl
Last active December 19, 2015 01:58
Show Gist options
  • Save Util/5879369 to your computer and use it in GitHub Desktop.
Save Util/5879369 to your computer and use it in GitHub Desktop.
A reply to the original (prefork) version.
use Data::Dumper;
my @files = (
map( "/data/switch/cdr/SWITCH_20130627_151${_}00", qw( 8 5 9 7 6 ) ),
map( "/data/switch/otherCDR/SWITCH_20130627_151${_}00", qw( 9 6 8 5 7 ) ),
);
print Dumper \@files;
my @sorted =
map { unpack 'x[N] a*' }
sort
map { /SWITCH_\d{8}_(\d{6})/ or die; pack 'N a*', $1, $_ }
@files;
print Dumper \@sorted;
use v6;
my @files =
<8 5 9 7 6>.map({ "/data/switch/cdr/SWITCH_20130627_151{$_}00"}),
<9 6 8 5 7>.map({"/data/switch/otherCDR/SWITCH_20130627_151{$_}00"});
say @files.perl;
my @sorted = @files.sort({ / SWITCH _ \d**8 _ (\d**6) / or die; $0; });
say @sorted.perl;
1. Spelling corrected: Schwartzian transform
2. No need to use parens around \d{8}.
3. I like ST better, but GRT is faster for large lists:
http://www.perlmonks.org/?node_id=145659
4. Need to warn or die to show bad data when regex fails.
5. Perl 6 automatically does ST for you:
my @sorted = @files.sort({ / SWITCH _ \d**8 _ (\d**6) / or die; $0; });
$ ./grt.pl # Same output as ST version
$VAR1 = [
'/data/switch/cdr/SWITCH_20130627_151800',
'/data/switch/cdr/SWITCH_20130627_151500',
'/data/switch/cdr/SWITCH_20130627_151900',
'/data/switch/cdr/SWITCH_20130627_151700',
'/data/switch/cdr/SWITCH_20130627_151600',
'/data/switch/otherCDR/SWITCH_20130627_151900',
'/data/switch/otherCDR/SWITCH_20130627_151600',
'/data/switch/otherCDR/SWITCH_20130627_151800',
'/data/switch/otherCDR/SWITCH_20130627_151500',
'/data/switch/otherCDR/SWITCH_20130627_151700'
];
$VAR1 = [
'/data/switch/cdr/SWITCH_20130627_151500',
'/data/switch/otherCDR/SWITCH_20130627_151500',
'/data/switch/cdr/SWITCH_20130627_151600',
'/data/switch/otherCDR/SWITCH_20130627_151600',
'/data/switch/cdr/SWITCH_20130627_151700',
'/data/switch/otherCDR/SWITCH_20130627_151700',
'/data/switch/cdr/SWITCH_20130627_151800',
'/data/switch/otherCDR/SWITCH_20130627_151800',
'/data/switch/cdr/SWITCH_20130627_151900',
'/data/switch/otherCDR/SWITCH_20130627_151900'
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment