Link to home
Start Free TrialLog in
Avatar of BalNigel
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...
ASKER CERTIFIED SOLUTION
Avatar of mjcoyne
mjcoyne

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BalNigel
BalNigel

ASKER

This is what I did eventually - was trying to be very lazy initially, but I will take the regex, it's useful.
foreach my $company_value (@groupoptions){
      company_value->[1] =~ s/The //;      
}

@groupoptions = sort { $a->[1] cmp $b ->[1]} @groupoptions;
Avatar of ozo
@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;