Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'd probably use a tied array.

#!perl use strict; use warnings; use feature 'say'; use Data::Dumper; BEGIN { package Array::CaseFolded; # Perl 5.16 has 'fc' feature; older Perls can just use 'lc'. use if $] >= 5.016, feature => 'fc'; BEGIN { $] < 5.016 and eval 'sub fc ($) { lc $_[0] }' }; # Copied from Tie::StdArray. # Altered STORE, PUSH, UNSHIFT, and SPLICE. sub TIEARRAY { bless [], $_[0] } sub DESTROY { } sub EXTEND { } sub FETCHSIZE { scalar @{$_[0]} } sub STORESIZE { $#{$_[0]} = $_[1]-1 } sub STORE { $_[0]->[$_[1]] = fc $_[2] } sub FETCH { $_[0]->[$_[1]] } sub CLEAR { @{$_[0]} = () } sub POP { pop( @{$_[0]} ) } sub PUSH { my $o = shift; push( @$o, map fc $_, @_ ) } sub SHIFT { shift( @{$_[0]} ) } sub UNSHIFT { my $o = shift; unshift( @$o, map fc $_, @_ ) } sub EXISTS { exists $_[0]->[$_[1]] } sub DELETE { delete $_[0]->[$_[1]] } sub SPLICE { my $ob = shift; my $sz = $ob->FETCHSIZE; my $off = @_ ? shift : 0; $off += $sz if $off < 0; my $len = @_ ? shift : $sz-$off; return splice( @$ob, $off, $len, map fc($_), @_ ); } # Utility functions sub contains { my ( $arr, $value ) = ( shift, fc shift ); !!grep $value eq $_, @$arr; } sub after { my ( $arr, $value ) = ( shift, fc shift ); my $found = -1; for my $i ( 0 .. $#$arr ) { next unless $arr->[$i] eq $value; $found = $i; last; } $found >= 0 and $found < $#$arr and return $arr->[ $found + 1 +]; return undef; } }; tie my @arr, 'Array::CaseFolded'; @arr = qw( Foo BAR baz ); print Dumper(\@arr); say tied(@arr)->contains( 'Bar' ); say tied(@arr)->after( 'Bar' );

In reply to Re: Module for sets of strings, ordered, case-insensitive? by tobyink
in thread Module for sets of strings, ordered, case-insensitive? by cxw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-12-08 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (34 votes). Check out past polls.

    Notices?