Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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.

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]
[Discipulus]: summon succeded! zentara is here!! ;=)
[LanX]: Saxana is still missing ...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2018-04-20 10:50 GMT
Find Nodes?
    Voting Booth?