Perl Sort Regex

Posted on 2007-07-26
Last Modified: 2008-02-07
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:
The company7
The company 6

Question by:BalNigel
    LVL 17

    Accepted Solution

    #!/usr/bin/perl -w
    use strict;

    my @original = (
        'The company7',
        'The company 6',

    my @sortable;

    for (@original) {
        push @sortable, ($_ =~ /(?:^The\s)?(.+)/);

    @sortable = sort {$a cmp $b} @sortable;

    print join "\n", @sortable;

    Author Comment

    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;
    LVL 84

    Expert Comment

    @groupoptions = (
    ["d","The company7"],
    ["a","The company 6"],
    @groupoptions = map{$_->[0]}
                    sort {$a->[1] cmp $b->[1]}
                    map {[$_,($_->[1]=~/^(The\s+)?(.*)/i)[1]]}

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Filtering a file to table 9 96
    Perl 101 11 63
    Removing file extension within a file. 4 81
    Rename file based on contents in the file? 3 61
    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now