in reply to Re^2: Module for sets of strings, ordered, case-insensitive?
in thread Module for sets of strings, ordered, case-insensitive?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Module for sets of strings, ordered, case-insensitive?
by cxw (Scribe) on Jan 10, 2021 at 21:16 UTC | |
by GrandFather (Saint) on Jan 11, 2021 at 00:18 UTC |