Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Matching the first letter in a string

by myocom (Deacon)
on Jul 31, 2001 at 02:49 UTC ( #101008=note: print w/replies, xml ) Need Help??

in reply to Matching the first letter in a string

If you're going to match a fixed number of letters in a fixed position (e.g., the first letter), use substr instead:

if (substr($title,0,1) eq $layer) { # do something... }

If you want the items checked regardless of case (that is, 'a' is treated the same as 'A'), force it to be lowercase:

if (lc(substr($title,0,1) eq lc($layer)) { # do something... }

As a side note, you're doing some extraneous quoting in there too, on the assignment to $title. I'd suggest changing that line to:

my $title = $dbrow{Title};

Replies are listed 'Best First'.
Re: Re: Matching the first letter in a string
by tomhukins (Curate) on Jan 07, 2002 at 21:48 UTC

    The original question concerned the first letter of $title, whereas your answer compares the first character.

    To match the first letter case insensitively, try:

    my $title="$dbrow{'Title'}"; $title =~ m/([a-z])/i; # Match the first letter in $title if (lc($1) eq lc($layer)) { #do something... }
    NB. I'm using [a-z] rather than \w because \w includes the underscore character, which isn't a letter.

    Given that you seem to be working with results returned from a database, it's almost certainly better to check the first letter matches in the SQL query, rather than in the Perl code later on. Try something like SELECT * FROM table WHERE field LIKE "A%" to match records where field starts with the letter A.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://101008]
[Eily]: oh no, how am I going to answer to that prince who wanted to leave me all this money?
[Eily]: and all those mails to enlarge my
[Eily]: inbox
[marto]: I'm happy to be the middle man :P
LanX found a bone xylophone for node reaper massage
[Veltro]: NodeReaper did not notice messages from Klaas Vaak, lucky Eily...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2018-05-23 14:43 GMT
Find Nodes?
    Voting Booth?