in reply to Module for sets of strings, ordered, case-insensitive?

It's not clear to me how you wish the ordering to work. The case insensitive matching can be done by populating a hash with the lower cased version of the match string then matching against the lower case version of the test string. Wrapping that process up as a class would be fairly trivial and may well be the basis for resolving the ordering problem.

My guess is that there are no modules that do just what you want unless this is a common problem. I can't tell if this is a common problem because the ordering requirements aren't obvious to me. The case insensitive matching is sufficiently trivial that it is unlikely to have been wrapped up in a module. Perhaps you could tell us more about the context of your problem?

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
  • Comment on Re: Module for sets of strings, ordered, case-insensitive?

Replies are listed 'Best First'.
Re^2: Module for sets of strings, ordered, case-insensitive?
by cxw (Scribe) on Dec 27, 2020 at 00:32 UTC

    Thanks for following up!

    • The order is the order given in the constructor - qw(first second third ...).
    • "sufficiently trivial" --- that would explain why I couldn't find a module.

    Use case: I am writing a build system (why not? :D ) that runs several phases of processing in order. I need to:

    • Move from one phase to its successor phase over the course of execution (therefore, $nextphase = $phases->after($thisphase); and
    • Map from a command-line parameter provided by the user to a phase, if the user wants to repeat a phase or skip ahead (therefore, case-insensitive lookup).

    I am using string phase names rather than phase numbers because I don't want the user to have to remember the numbers.

      Something like:

      use warnings; use strict; package PhaseManager; sub new { my ($class, @phases) = @_; my %phaseHash = map {lc($phases[$_]) => lc($phases[$_ + 1] || '')} 0 .. $#phases; return bless \%phaseHash, $class; } sub Next { my ($self, $phase) = @_; die "Unknown phase '$phase'" if !exists $self->{lc $phase}; return $self->{lc $phase}; } package main; my $manager = PhaseManager->new (qw'first second last'); my @testPhases = qw'first Second LAST FUBAR'; for my $currPhase (@testPhases) { my $nextPhase = $manager->Next($currPhase) || '<No next phase>'; print "Phase '$currPhase' goes to '$nextPhase'\n"; }

      Prints:

      Phase 'first' goes to 'second' Phase 'Second' goes to 'last' Phase 'LAST' goes to '<No next phase>' Unknown phase 'FUBAR' at D:\Delme~~\PerlScratch\delme.pl line 17.
      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        I like this approach --- very clean. May I bundle it for CPAN? If so, standard Perl license OK? (I checked your userpage but didn't see a license statement.) Thanks!