#!/opt/local/bin/perl
use strict;
use warnings;
open my $vlan_in, '<', shift @ARGV;
my @vlans = <$vlan_in>;
foreach my $line (@vlans) {
chomp $line;
my @items = ($line =~ /( # capture:
\d # digit characters
+ # one or more in a row
)/gx ); # as many as you can find
foreach my $item (@items) {
next unless defined $item;
$item =~ s/,//;
print "vlan $item\n";
}
}
####
#!/opt/local/bin/perl
use strict;
use warnings;
open my $vlan_in, '<', shift @ARGV;
my @vlans = <$vlan_in>;
foreach my $line (@vlans) {
chomp $line;
# Separate the 'vlan' from the list (and throw it away).
my (undef, $vlans) = split /\s+/, $line;
# Break up the list into items.
my @items = split /,/, $vlans;
# Print your new output.
foreach my $item (@items) {
next unless defined $item;
$item =~ s/,//;
print "vlan $item\n";
}
}
####
use Benchmark qw(:all);
my @lines = split /\n/, < sub {
my @copy = @lines;
foreach my $line (@copy) {
my (undef, $vlans) = split /\s+/, $line;
my @items = split /,/, $vlans;
}
},
'/g' => sub {
my @copy = @lines;
foreach my $line (@copy) {
my @items = ($line =~ /(\d+)/g);
}
},
'sub-split' => sub {
my @copy = @lines;
foreach my $line (@copy) {
s/vlan //;
my @items = split /,/, $line;
}
},
}
);
####
Rate split-split /g sub-split
split-split 104167/s -- -8% -22%
/g 113636/s 9% -- -15%
sub-split 133333/s 28% 17% --
####
Rate split-split /g sub-split
split-split 102459/s -- -11% -26%
/g 115741/s 13% -- -16%
sub-split 138504/s 35% 20% --
####
Rate split-split /g sub-split
split-split 100503/s -- -12% -28%
/g 114286/s 14% -- -18%
sub-split 138889/s 38% 22% --
####
Rate split-split /g sub-split
split-split 102480/s -- -10% -24%
/g 114495/s 12% -- -15%
sub-split 134590/s 31% 18% --