Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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 imbibing at the Monastery: (8)
As of 2015-07-06 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls