Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: japhygesis

by Abigail (Deacon)
on Jun 22, 2001 at 05:51 UTC ( [id://90609]=note: print w/replies, xml ) Need Help??


in reply to japhygesis

What follows is a module I wrote quite some time ago (as a proof of concept) that "fixes" index in a different way. It returns undef if there is no match, and the position of the match otherwise - however, if it matches at the beginning of the string, "0 but true" is returned.
package Perl; use Exporter; use vars qw /@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/; @ISA = qw /Exporter/; @EXPORT = qw /index/; @EXPORT_OK = qw //; %EXPORT_TAGS = (); sub index { my $result; $result = 2 == @_ ? CORE::index ($_ [0], $_ [1]) : CORE::index ($_ [0], $_ [1], $_ [2]); $result < 0 ? undef : $result || "00"; } 1; __END__

-- Abigail

Replies are listed 'Best First'.
Re: Re: japhygesis
by srawls (Friar) on Jun 22, 2001 at 21:59 UTC
    Nice, too few people realise that you can just return a string to evaluate as zero, but still be true. I often return things like "0 but true" when I want the 0 value to be true; that way you can return 0 on errors too. A little trick I picked up was if you need to print the value, you can just use "0\0" It's 0 plus the null character, so it's true, but it only prints 0.

    The 15 year old, freshman programmer,
    Stephen Rawls

      #!/usr/bin/perl -w print 0+"0 but true",$/; print 0+"0\0",$/; __END__ Argument "0" isn't numeric in addition (+) at - line 3 0 0

      Not only does it generate warnings, but they are confusing warnings. I'd probably use "00" over "0\0" if you wanted something that printed out nicer. (:

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://90609]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-19 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found