BalNigel
asked on
Perl Sort Regex
I have the task of sorting a list - not a problem
@groupoptions = sort { $a->[1] cmp $b ->[1]} @groupoptions;
what I have to do is then remove the word "The" from the sort and sort on the second word of the list.
Is there a way of doing it with a regex within the sort where I could use something like $string =~ s/The//g; ?
I would have values like:
company10
company1
The company7
The company 6
etc...
@groupoptions = sort { $a->[1] cmp $b ->[1]} @groupoptions;
what I have to do is then remove the word "The" from the sort and sort on the second word of the list.
Is there a way of doing it with a regex within the sort where I could use something like $string =~ s/The//g; ?
I would have values like:
company10
company1
The company7
The company 6
etc...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@groupoptions = (
["c","company10"],
["b","company1"],
["d","The company7"],
["a","The company 6"],
);
@groupoptions = map{$_->[0]}
sort {$a->[1] cmp $b->[1]}
map {[$_,($_->[1]=~/^(The\s+)? (.*)/i)[1] ]}
@groupoptions;
["c","company10"],
["b","company1"],
["d","The company7"],
["a","The company 6"],
);
@groupoptions = map{$_->[0]}
sort {$a->[1] cmp $b->[1]}
map {[$_,($_->[1]=~/^(The\s+)?
@groupoptions;
ASKER
foreach my $company_value (@groupoptions){
company_value->[1] =~ s/The //;
}
@groupoptions = sort { $a->[1] cmp $b ->[1]} @groupoptions;