Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: splitting a word

by Parham (Friar)
on Jan 16, 2002 at 02:36 UTC ( #139065=note: print w/ replies, xml ) Need Help??


in reply to splitting a word

my $firstname = 'Parham'; #set first name $firstname =~ /^(\w)/; #grab the first letter print $1; #print the first letter
i think the syntax of:
my ($variable) = $variable =~ /regex/;
disregards the purpose of the $(number) variables which is why i stay away from them.


Comment on Re: splitting a word
Select or Download Code
Re: Re: splitting a word
by Hofmator (Curate) on Jan 16, 2002 at 17:43 UTC

    I think the syntax ($match) = $var =~ /regex/;

    is a concise and flexible way to extract parts of a string. The following code should illustrate this nicely - note especially the usage with the /g modifier.

    #!/usr/bin/perl -w use strict; $_ = q/one two three four five/; my ($match, @matches); ($match) = /\w+\W+(\w+)/; print $match, "\n"; # prints two @matches = /(\w+)\W+(\w+)/; print join('|', @matches), "\n"; # prints one|two @matches = /(\w+)\W+(\w+)/g; print join('|', @matches), "\n"; # prints one|two|three|four @matches = /(\w+)/g; print join('|', @matches), "\n"; # prints one|two|three|four|five

    -- Hofmator

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-07-25 03:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls