Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Process string as Array

by Anonymous Monk
on May 28, 2004 at 19:04 UTC ( #357342=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to process a string as an array to validate data and I don't have a perl book handy to point me in the right direction?
How do I look at a string byte for byte?

$mystrng = 'smith and jones';
?? #-- please help me here

Comment on Process string as Array
Re: Process string as Array
by duff (Vicar) on May 28, 2004 at 19:15 UTC
    perldoc -f substr

    Alternatively you can use split but if you want changes to the array to be reflected back in the string, you'll need to reform the string:

    @string = split //, $string; # manipulate @string all you want $string = join '', @string;
    update: BTW, I forgot to mention, if you're trying to validate your data as something generally well known, there are modules on CPAN that can probably do it for you.
Re: Process string as Array
by waswas-fng (Curate) on May 28, 2004 at 19:27 UTC
    What are you trying to validate? Most of the time you can use a regex to validate without breaking the string. Give us some examples and you may get a "better" way to do it.


    -Waswas
Re: Process string as Array
by dragonchild (Archbishop) on May 28, 2004 at 19:29 UTC
    my @string_as_array = split( '', $mystring ); # Do stuff here $mystring = join( '', @string_as_array );

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Process string as Array
by Fletch (Chancellor) on May 28, 2004 at 19:30 UTC

    Many times if you're trying to muck with a string character by character you're probably not thinking in a perl-y manner yet (you're thinking C-ish and trying to iterate over a string rather than just using m// or s/// to do whatever in a single operation). If you're really set on doing it char by char, you want substr, split, or maybe something like Tie::CharArray.

Re: Process string as Array
by BrowserUk (Pope) on May 28, 2004 at 20:12 UTC

    As others have said, it's very rare that you need to process string byte by byte in perl, but if you really do have to and you have any volume of strings to process then use  @bytes = unpack 'C*', $string; in preference to  @bytes = split '', $string; . It's an order of magnitude quicker.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Process string as Array
by ambrus (Abbot) on May 29, 2004 at 10:04 UTC
    while ($mystring=~ /(.)/gs) { my $char= $1; &do_whatever_with ($char); }
    provided you mean character-by-character if the string is unicode.
Re: Process string as Array
by zentara (Archbishop) on May 29, 2004 at 13:33 UTC
    Here's a handy code fragment I was just looking at today.
    $string = 'foo' x 2048; $n = 1024; # $n is group size. @groups = unpack "a$n" x ((length($string)/$n)-1) . "a*", $string; print join("\n\n\n",@groups),"\n\n";

    I'm not really a human, but I play one on earth. flash japh

      Recent changes to unpack (added in 5.8.0) have made splitting a string into groups much easier.

      $string = "foo" x 3 . "fo"; print join(" -- ", unpack "(A3)*", $string), "\n";

      Though your code is slightly different; the final "fo" is tacked onto the end of the last element, whereas with the above it's a new element.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://357342]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-07-29 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (226 votes), past polls