Author |
Message |
Tk So #1 / 15
|
 How to extract first letter in string?
Quote:
> > Are there anyone, who can tell me how to extract the first letter in a > > string.. > Use: > $firstLetter = substr($string, 1, 1); > /Daniel
If you haven't messed with $[ (have you?), I think it should be: $firstLetter = substr($string, 0, 1); More info on: perldoc -f substr -TK-
|
Tue, 13 Feb 2001 03:00:00 GMT |
|
 |
Thomas Albec #2 / 15
|
 How to extract first letter in string?
Hi there, Are there anyone, who can tell me how to extract the first letter in a string.. Thanks, Thomas -- Product Specialist, Alias|Wavefront Assist Denmark PowerAnimator Level 2 -- Maya Caracter Animation -------------------------------------------------- Crystal Graphics Tel. +45 54 70 71 10 Guldborgvej 3 Mob. +45 20 40 12 03 DK-4990 Saksk?bing Fax. +45 54 70 03 10
|
Wed, 14 Feb 2001 03:00:00 GMT |
|
 |
Daniel Stridhamma #3 / 15
|
 How to extract first letter in string?
Quote: > Are there anyone, who can tell me how to extract the first letter in a > string..
Use: $firstLetter = substr($string, 1, 1); /Daniel
|
Wed, 14 Feb 2001 03:00:00 GMT |
|
 |
Craig Ber #4 / 15
|
 How to extract first letter in string?
: > Are there anyone, who can tell me how to extract the first letter in a : > string.. : : Use: : : $firstLetter = substr($string, 1, 1); And scratch your head in puzzlement as to why $firstLetter contains the second letter in the string. :) ---------------------------------------------------------------------
--*-- Home Page: http://www.cinenet.net/users/cberry/home.html | "Ripple in still water, when there is no pebble tossed, nor wind to blow..."
|
Wed, 14 Feb 2001 03:00:00 GMT |
|
 |
Jonathan Sto #5 / 15
|
 How to extract first letter in string?
On Sat, 29 Aug 1998 04:11:56 -0700, Thomas Albech wrote : Quote: >Hi there, >Are there anyone, who can tell me how to extract the first letter in a >string..
C'mon guy that aint even taxing to my seagulls: #!perl $string = "This is the first character"; $string =~ /(^.{1})/; print $1,"\n"; $char = substr($string,0,1); print $char,"\n";
__END__ And thats before deploying pack and sundry other little niceties. We'll let the benchmark boys decide which one of these is most efficient. Next time read the FAQ its much more fun than getting flamed up here. /J\ -- Jonathan Stowe Some of your questions answered: <URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
|
Wed, 14 Feb 2001 03:00:00 GMT |
|
 |
Rick Delane #6 / 15
|
 How to extract first letter in string?
Quote:
> #!perl > $string = "This is the first character"; > $string =~ /(^.{1})/; > print $1,"\n"; > $char = substr($string,0,1); > print $char,"\n";
You mean $char[0], of course. Quote: > __END__ > And thats before deploying pack and sundry other little niceties. > We'll let the benchmark boys decide which one of these is most > efficient.
I won't do all of them because we all know substr will win. Or will it with 5.005? (I don't have it yet.) I was curious about the first one, though. use Benchmark; timethese(1000000, { SPURIOUS => sub { "This is the first character" =~ /(^.{1})/ }, NORMAL => sub { "This is the first character" =~ /^(.)/ }, } ) __END__ Benchmark: timing 1000000 iterations of NORMAL, SPURIOUS... NORMAL: 14 secs (15.24 usr 0.00 sys = 15.24 cpu) SPURIOUS: 18 secs (17.62 usr 0.00 sys = 17.62 cpu) Probably not as meaningful as the extra second to type it. -- Rick Delaney
|
Wed, 14 Feb 2001 03:00:00 GMT |
|
 |
Tk So #7 / 15
|
 How to extract first letter in string?
Quote:
> >> Are there anyone, who can tell me how to extract the first letter in a > >> string.. > Might something like > ($firstLetter) = $string =~ /([^\W\d_])/; > be better if the original poster really wanted the first letter, not the > first character?
You mean first 'letter', as in /([a-zA-Z])/, right? I have difficulty in translating /([^\W\d_])/ into English - perhaps the problem is with my English lang. :) -TK-
|
Wed, 14 Feb 2001 03:00:00 GMT |
|
 |
Mike St #8 / 15
|
 How to extract first letter in string?
Quote: >> Are there anyone, who can tell me how to extract the first letter in a >> string.. >Use: >$firstLetter = substr($string, 1, 1);
Might something like ($firstLetter) = $string =~ /([^\W\d_])/; be better if the original poster really wanted the first letter, not the first character? Mike --
http://www.stok.co.uk/~mike/ | PGP fingerprint FE 56 4D 7D 42 1A 4A 9C http://www.tiac.net/users/stok/ | 65 F3 3F 1D 27 22 B7 41
|
Thu, 15 Feb 2001 03:00:00 GMT |
|
 |
Jonathan Sto #9 / 15
|
 How to extract first letter in string?
On Sat, 29 Aug 1998 22:25:48 GMT, Rick Delaney wrote : Quote:
<snip>
>You mean $char[0], of course.
I meant $char[0], of course. Quote: >use Benchmark; >timethese(1000000, > { > SPURIOUS => sub { "This is the first character" =~ /(^.{1})/ }, > NORMAL => sub { "This is the first character" =~ /^(.)/ }, > } >) >__END__ >Benchmark: timing 1000000 iterations of NORMAL, SPURIOUS... > NORMAL: 14 secs (15.24 usr 0.00 sys = 15.24 cpu) > SPURIOUS: 18 secs (17.62 usr 0.00 sys = 17.62 cpu)
This I do find interesting - I would have thought that the two would have resolved to the same thing or does the regex engine build the entire mechanism to deal with any values between the {} regardless ? Look at the code and take some headache pills I guess. Quote: >Probably not as meaningful as the extra second to type it.
And I thought my typing was slow. /J\ -- Jonathan Stowe Some of your questions answered: <URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
|
Thu, 15 Feb 2001 03:00:00 GMT |
|
 |
Daniel Stridhamma #10 / 15
|
 How to extract first letter in string?
Of course. You're right! Sorry for my typo... Quote: > > > Are there anyone, who can tell me how to extract the first letter in a > > > string.. > > Use: > > $firstLetter = substr($string, 1, 1); > > /Daniel > If you haven't messed with $[ (have you?), I think it should be: > $firstLetter = substr($string, 0, 1); > More info on: > perldoc -f substr > -TK-
|
Thu, 15 Feb 2001 03:00:00 GMT |
|
 |
Larry Rosl #11 / 15
|
 How to extract first letter in string?
[Posted to comp.lang.perl.misc and copy mailed.]
Quote:
... > > Might something like > > ($firstLetter) = $string =~ /([^\W\d_])/; > > be better if the original poster really wanted the first letter, not the > > first character? > You mean first 'letter', as in /([a-zA-Z])/, right? > I have difficulty in translating /([^\W\d_])/ into English - perhaps > the problem is with my English lang. :)
He meant first 'letter', as in /(whatever characters the current locale defines as being included in [\w], less the decimal digits and underscore)/. By default, that includes only the limited set of letters [a-zA-Z] defined in the ASCII character set (where the A stands for American, remember). But even in one-byte-land, there are *many* others in specific locales. See `perldoc perllocale`. -- (Just Another Larry) Rosler Hewlett-Packard Laboratories http://www.hpl.hp.com/personal/Larry_Rosler/
|
Thu, 15 Feb 2001 03:00:00 GMT |
|
 |
Mike St #12 / 15
|
 How to extract first letter in string?
Quote:
>> >> Are there anyone, who can tell me how to extract the first letter in a >> >> string.. >> Might something like >> ($firstLetter) = $string =~ /([^\W\d_])/; >> be better if the original poster really wanted the first letter, not the >> first character? >You mean first 'letter', as in /([a-zA-Z])/, right? >I have difficulty in translating /([^\W\d_])/ into English - perhaps >the problem is with my English lang. :)
If your only concern is ASCII then you'll be OK assuming [a-zA-Z] covers all the letters, but the perllocale man page says this: These are important issues, especially for languages other than English--but also for English: it would be naieve to imagine that A-Za-z defines all the "letters" needed to write in English. \w and \W are locale aware, so the set of characters matched by [\W\d_] are any characters which aren't alphabetic, numeric or _ in the current locale (\W), any characters which are numeric (\d) and the _. That's everything *except* alphabetic characters, so [^\W\d_] matches just alphabetic characters. If you'll forgive the "non word" in the example (assuming you're reading on an ISO8859-1 display): #!/usr/local/bin/perl -w use locale; use POSIX qw/locale_h/; $string = '?ne'; ($firstLetter) = $string =~ /([^\W\d_])/; print "$firstLetter\n"; setlocale (LC_CTYPE, 'fr_FR.ISO8859-1') || die "selocale ($!)\n"; ($firstLetter) = $string =~ /([^\W\d_])/; print "$firstLetter\n"; __END__ produces n ? which seems reasonable, my default setup uses ASCII which doesn't consider o-circumflex a letter. This may or may not be useful considering the
Hope this helps, Mike --
http://www.stok.co.uk/~mike/ | PGP fingerprint FE 56 4D 7D 42 1A 4A 9C http://www.tiac.net/users/stok/ | 65 F3 3F 1D 27 22 B7 41
|
Thu, 15 Feb 2001 03:00:00 GMT |
|
 |
Tom Phoeni #13 / 15
|
 How to extract first letter in string?
Quote: > what if I wanted to search out a keyword, within a string, or text > file, and change that keyword to something else, like make it BOLD > faced before displaying it to the page? > How would you write this? Use split?
I'd probably use s///. Hope this helps! -- Tom Phoenix Perl Training and Hacking Esperanto Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
|
Fri, 16 Feb 2001 03:00:00 GMT |
|
 |
Albert W. Dorringt #14 / 15
|
 How to extract first letter in string?
:> To expand on this a bit, what if I wanted to search out a keyword, within a :> string, or text file, and change that keyword to something else, like make it :> BOLD faced before displaying it to the page? :> :> How would you write this? Use split? :> Sed could probably do this faster than perl, but something like: $keyword = blah; $cap_keyword = uc $keyword; # UPPER CASE keyword $string =~ s/$keyword/$cap_keyword/g; would be fairly simple. - Al -- Al Dorrington FIRMS & Web Admin, Oracle DBA Phone: 765-451-9655 IC-DELCO CIM, Delphi Delco Electronics Systems Fax: 765-451-8230
|
Sat, 17 Feb 2001 03:00:00 GMT |
|
|