Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

String Splitting

by Bugorr (Novice)
on Aug 18, 2005 at 18:05 UTC ( [id://484897]=perlquestion: print w/replies, xml ) Need Help??

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

Hello. I have a very simple questions, which could be done with simpliest regex, but I'm new to it and can not get my way around it yet. Anyhow the questions is, I have a line with 2 strings separated by a \t. What can I do to assign first string to the first variable and second string to the second variable. Thank you

Replies are listed 'Best First'.
Re: String Splitting
by Transient (Hermit) on Aug 18, 2005 at 18:07 UTC
    You already have the answer in your thread title =)

    split

    P.S. If you want more details, possibly post what you have tried... it goes a long way to show effort.
Re: String Splitting
by ikegami (Patriarch) on Aug 18, 2005 at 18:15 UTC
    ($var1, $var2) = split("\t", $string);
    or if you haven't declared the two variables yet:
    my ($var1, $var2) = split("\t", $string);
Re: String Splitting
by friedo (Prior) on Aug 18, 2005 at 18:15 UTC
    You can use the aptly named split.

    my ($var1, $var2) = split /\t/, "string with\ttabs";
Re: String Splitting
by GrandFather (Saint) on Aug 18, 2005 at 18:51 UTC

    or if you really want to do it with a regex:

    my ($str1, $str2) = $line =~ /^(.*?)\t(.*)/;

    Note that this splits at the first tab. Remove the ? and it will split at the last tab. Note also that the solutions using split actually generate a list with one entry per tab plus one. The regex can only generate a list with two entries. See perlretut.


    Perl is Huffman encoded by design.
      or equivalently (to the one listed above, with the '?'):
      my ( $str1, $str2 ) = split(/\t/, $line, 2);
      (so much for showing effort ;) (update - meant this in general, not directed at GrandFather!))
Re: String Splitting
by sk (Curate) on Aug 18, 2005 at 18:17 UTC
    #!/usr/bin/perl -w use strict; my $str = "first\tsecond"; my ($first,$second) = (); ($first,$second) = split (/\t/,$str); print ("First = $first, Second = $second\n"); __END__ First = first, Second = second

    Please check perldoc -f split

    -SK

Re: String Splitting
by pbeckingham (Parson) on Aug 18, 2005 at 19:03 UTC

    If there are only two lines, there is no need to split.

    my ($one, $two) = <$input>;
    In list context, the <> operator returns a list.

    Update: I completely misunderstood the question.



    pbeckingham - typist, perishable vertebrate.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-25 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found