Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How to get each character in string value

by Punitha (Priest)
on Dec 02, 2008 at 07:08 UTC ( #727319=note: print w/ replies, xml ) Need Help??


in reply to How to get each character in string value

You may want something like this?

use strict; use warnings; my $string = "Hello World"; while($string =~/./g){ print "$&\n"; }

Or,

use strict; use warnings; my $string = "Hello World"; my @string = split(//,$string); for my $str (@string){ print "$str\n"; }

Punitha


Comment on Re: How to get each character in string value
Select or Download Code
Re^2: How to get each character in string value
by linuxer (Deacon) on Dec 02, 2008 at 09:26 UTC

    According to perldoc perldata (paragraph for $MATCH) I would avoid the use of "$&"; I would capture the pattern and use "$1" instead.

    use strict; use warnings; my $string = "Hello World"; while($string =~/(.)/g){ print "$1\n"; }
    But usually I prefer the split() solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://727319]
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 07:38 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