Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

string spliting

by andromeda08 (Initiate)
on Feb 12, 2009 at 11:48 UTC ( #743279=perlquestion: print w/replies, xml ) Need Help??

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

i have a variable that contains 12 letters and i want to split it in two other variables so that one variable contains first 6 letters and the other next 6 letters. how to do it?

Replies are listed 'Best First'.
Re: string spliting
by why_bird (Pilgrim) on Feb 12, 2009 at 11:54 UTC

    Check out substr. If you've got more questions, come back and ask for more help. Also, there's lots on the web on all sorts of ways of manipulating perl strings. When you find the function you need, look it up on perldoc, a very handy reference that I wish I'd discovered sooner! Also try SuperSearch, since it's quite likely someone else has already asked the same question.

    People here are more likely to help, I've found, if you tell them what steps you've already taken to try and solve your own problem.

    Update: more references.
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
      $v = '1234567890AB'; ($a, $b) = unpack('A6 A6', $v) ; print "$v\n$a\n$b";
Re: string spliting
by hbm (Hermit) on Feb 12, 2009 at 17:08 UTC

    Assuming you want to guarantee 12 letters, I'd use a regular expression:

    $_ = 'abcdefghijkl'; my ($a,$b) = (/^([a-z]{6})([a-z]{6})$/i) ? ($1,$2) : (undef)x2; print "$a, $b\n" if defined $a;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2023-06-06 20:30 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.