Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to get each character in string value

by poolpi (Hermit)
on Dec 02, 2008 at 09:45 UTC ( [id://727343]=note: print w/replies, xml ) Need Help??


in reply to How to get each character in string value

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @letters = unpack( "(a)*", 'Hello World' ); print Dumper \@letters; __END__ Output:$VAR1 = [ 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' ];

hth,
PooLpi

'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Replies are listed 'Best First'.
Re^2: How to get each character in string value
by spmlingam (Scribe) on Dec 02, 2008 at 11:06 UTC
    If you want the output as array, you can use split and unpack, otherwise you can use pattern matching with /g flag in a while loop.
    example with unpack & character by character processing
    $string="Hello World"; $sum = 0; foreach $ascval (unpack("C*", $string)) { $sum += $ascval; } print "sum is $sum\n"; # prints "1052"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-25 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found