Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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
Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2015-11-29 10:18 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (750 votes), past polls