Splitting a string with placeholders

Discussion in 'Perl Misc' started by Bryan, Apr 13, 2007.

  1. Bryan

    Bryan Guest

    I have a string like this:
    my $string = "A\tB\tC\t\t\t\t\t\t\n";

    When I do:
    my @array = split("\t", $string);

    I get the following array:
    @array = ["A", "B", "C"];

    The placeholding '\t's are lost.

    How can I keep these so that my array is like this:
    @array = ["A", "B", "C", "", "", "", "", ""];

    Thanks,
    B
     
    Bryan, Apr 13, 2007
    #1
    1. Advertising

  2. Bryan wrote:
    > I have a string like this:
    > my $string = "A\tB\tC\t\t\t\t\t\t\n";
    >
    > When I do:
    > my @array = split("\t", $string);
    >
    > I get the following array:
    > @array = ["A", "B", "C"];
    >
    > The placeholding '\t's are lost.
    >
    > How can I keep these so that my array is like this:
    > @array = ["A", "B", "C", "", "", "", "", ""];


    my @array = split /\t/, $string, -1;



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Apr 13, 2007
    #2
    1. Advertising

  3. Bryan wrote:
    > I have a string like this:
    > my $string = "A\tB\tC\t\t\t\t\t\t\n";
    >
    > When I do:
    > my @array = split("\t", $string);
    >
    > I get the following array:
    > @array = ["A", "B", "C"];
    >
    > The placeholding '\t's are lost.
    >
    > How can I keep these so that my array is like this:
    > @array = ["A", "B", "C", "", "", "", "", ""];


    Did you check the documentation of split()?
    <quote>
    split /PATTERN/,EXPR,LIMIT
    [...] If LIMIT is
    unspecified or zero, trailing null fields are stripped [...]
    If LIMIT is
    negative, it is treated as if an arbitrarily large LIMIT had
    been specified.
    </quote>

    jue
     
    Jürgen Exner, Apr 13, 2007
    #3
  4. Bryan

    Mirco Wahab Guest

    Jürgen Exner wrote:
    > Bryan wrote:
    >> my $string = "A\tB\tC\t\t\t\t\t\t\n";

    > Did you check the documentation of split()?
    > ...
    > unspecified or zero, trailing null fields are stripped [...]
    > If LIMIT is
    > negative, it is treated as if an arbitrarily large LIMIT had
    > ...


    If I'm not mistaken, there's no 'trailing null field' here,
    because it's an un-chomped string ...

    Regards

    M.
     
    Mirco Wahab, Apr 13, 2007
    #4
  5. Bryan

    Bryan Guest

    John W. Krahn wrote:
    > Bryan wrote:
    >> I have a string like this:
    >> my $string = "A\tB\tC\t\t\t\t\t\t\n";
    >>
    >> When I do:
    >> my @array = split("\t", $string);
    >>
    >> I get the following array:
    >> @array = ["A", "B", "C"];
    >>
    >> The placeholding '\t's are lost.
    >>
    >> How can I keep these so that my array is like this:
    >> @array = ["A", "B", "C", "", "", "", "", ""];

    >
    > my @array = split /\t/, $string, -1;
    >
    >
    >
    > John


    Thanks John,

    I need to omit the placeholder for the \n however. How do I do that?

    BTW, where did you find the -1 thing? Its not in the perldoc -f split
    info...

    Cheers,
    Bryan
     
    Bryan, Apr 13, 2007
    #5
  6. Bryan

    Bryan Guest

    Bryan wrote:
    > John W. Krahn wrote:
    >> Bryan wrote:
    >>> I have a string like this:
    >>> my $string = "A\tB\tC\t\t\t\t\t\t\n";
    >>>
    >>> When I do:
    >>> my @array = split("\t", $string);
    >>>
    >>> I get the following array:
    >>> @array = ["A", "B", "C"];
    >>>
    >>> The placeholding '\t's are lost.
    >>>
    >>> How can I keep these so that my array is like this:
    >>> @array = ["A", "B", "C", "", "", "", "", ""];

    >>
    >> my @array = split /\t/, $string, -1;
    >>
    >>
    >>
    >> John

    >
    > Thanks John,
    >
    > I need to omit the placeholder for the \n however. How do I do that?
    >
    > BTW, where did you find the -1 thing? Its not in the perldoc -f split
    > info...
    >
    > Cheers,
    > Bryan


    Oh duh. Found it.
     
    Bryan, Apr 13, 2007
    #6
  7. Bryan

    Scott Bryce Guest

    Bryan wrote:

    > I need to omit the placeholder for the \n however. How do I do that?


    Chomp $string before you split it.
     
    Scott Bryce, Apr 13, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Ericson
    Replies:
    0
    Views:
    428
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    442
  3. John Dibling
    Replies:
    0
    Views:
    417
    John Dibling
    Jul 19, 2003
  4. Michal Ludvig
    Replies:
    1
    Views:
    294
    Steven D'Aprano
    Feb 11, 2010
  5. Eric S. Johansson
    Replies:
    4
    Views:
    86
    Mark Lawrence
    Jan 17, 2014
Loading...

Share This Page