Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

String into Array

by cdherold (Monk)
on Feb 11, 2002 at 01:51 UTC ( [id://144555]=perlquestion: print w/replies, xml ) Need Help??

cdherold has asked for the wisdom of the Perl Monks concerning the following question:

Hello friends,

It's simple enough to put a string into an array using split() ... but that's if you've got something to split on ... like say a space or a "," ... or even a word boundry. But what do you do when all you've got is a string of characters ... say a long DNA sequence something like ... AGTCGTCGATGCTGATGCTAGCCCC ... ? i was looking for a "letter boundry" type of anchor (something similar to word boundry - \b), but couldn't find one. Did i miss something? If not, how do i split this string into an array (one letter for each element in the array)?

thanks,

chris

Replies are listed 'Best First'.
Re: String into Array
by BeernuT (Pilgrim) on Feb 11, 2002 at 01:56 UTC
    if you just want 1 letter per element then simply do
    $string = 'hello'; @array = split(//, $string);

    hope this helps

    -bn

    -----BEGIN PERL GEEK CODE BLOCK----- Version: 0.01 P+>+++( )$c->++P6 R++>+M >+$O >++MA+>++E >+PU >++BD >+++C+$D++$S+++>++++$X!WP+> ( )MO!PP++++n+>++CO-->+++PO-o >++G >+++A >+++OL!OLC+>++OLR+>++Ee!Ev!Eon+uL+++>*$ uB+$uS+$uH+$uo+$w-->---m! ------END PERL GEEK CODE BLOCK------
Re: String into Array
by blakem (Monsignor) on Feb 11, 2002 at 02:17 UTC
    You can use global matching /./g to process N chars at-a-time.
    #!/usr/bin/perl -wT use strict; my $string = 'AGTCGTCGATGCTGATGCTAGCCCC'; print "one at-a-time: $_\n" for $string =~ /./g; print "two at-a-time: $_\n" for $string =~ /.{1,2}/g; print "six at-a-time: $_\n" for $string =~ /.{1,6}/g; __END__ =head1 OUTPUT one at-a-time: A one at-a-time: G one at-a-time: T one at-a-time: C one at-a-time: G one at-a-time: T one at-a-time: C one at-a-time: G one at-a-time: A one at-a-time: T one at-a-time: G one at-a-time: C one at-a-time: T one at-a-time: G one at-a-time: A one at-a-time: T one at-a-time: G one at-a-time: C one at-a-time: T one at-a-time: A one at-a-time: G one at-a-time: C one at-a-time: C one at-a-time: C one at-a-time: C two at-a-time: AG two at-a-time: TC two at-a-time: GT two at-a-time: CG two at-a-time: AT two at-a-time: GC two at-a-time: TG two at-a-time: AT two at-a-time: GC two at-a-time: TA two at-a-time: GC two at-a-time: CC two at-a-time: C six at-a-time: AGTCGT six at-a-time: CGATGC six at-a-time: TGATGC six at-a-time: TAGCCC six at-a-time: C

    -Blake

Re: String into Array
by rob_au (Abbot) on Feb 11, 2002 at 01:59 UTC
    I'm not quite sure what you are after but you will find that split //, $string will split the string making each character a separate element of the returned list.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: String into Array
by chromatic (Archbishop) on Feb 11, 2002 at 01:56 UTC
    Split on ''.
Re: String into Array
by little (Curate) on Feb 11, 2002 at 01:56 UTC
    @results = split (/\s*/,$string);
    matches zero or more times
    Have a nice day
    All decision is left to your taste
Re: String into Array
by particle (Vicar) on Feb 11, 2002 at 13:36 UTC
    there is something magical about split. when used as my @chars = split //, $word;, it will split on each character, including a null field for each leading whitespace character. when used as my @chars = split '', $word;, it will split on each character without creating leading null fields.

    this may not be applicable to your data, but i thought it's something you would want to know.

    ~Particle

Re: String into Array
by hopes (Friar) on Feb 11, 2002 at 18:51 UTC
    Just one alternative chunk of code, without using split:
    You can use unpack and obtain one array which contains the ascii values of the strings:
    unpack "C*",$str
    And you can obtain the chars as follow
    @a=map chr,unpack "C*",$str;


    Hopes
    $_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s, $_^=q,$\^-]!,,print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found