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", "", "", "", "", ""];

    Bryan, Apr 13, 2007
    1. Advertisements

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

    John W. Krahn, Apr 13, 2007
    1. Advertisements

  3. Did you check the documentation of split()?
    [...] 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.

    Jürgen Exner, Apr 13, 2007
  4. Bryan

    Mirco Wahab Guest

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


    Mirco Wahab, Apr 13, 2007
  5. Bryan

    Bryan Guest

    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

    Bryan, Apr 13, 2007
  6. Bryan

    Bryan Guest

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

    Scott Bryce Guest

    Chomp $string before you split it.
    Scott Bryce, Apr 13, 2007
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.