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

I have a string and wanted to split it at the first space. Doing that part with split was easy, but then I thought of another format the string could be would brake the code because some of the data would have spaces in it.

The first code with 'that=x x' being the format error:
my $string = <<HTML; this=x that=x x another=x thing=x HTML my @array2 = split(/\s/, $string); print $_."\n" foreach (@array2);

Im going to answer my own question but I wanted to show this method and ask if anyone else knows another way to format the string the same way in maybe less lines.

The second code with the fix for the above issue:
my $string = <<HTML; this=x that=x x another=x thing=x HTML $string =~ s/ (\w+\=)/ \|$1/g; my @array2 = split(/\s\|/, $string); print $_."\n" foreach (@array2);

Replies are listed 'Best First'.
Re: Split this string
by hbm (Hermit) on Oct 12, 2011 at 12:36 UTC
    my $string = <<HTML; this=x that=x x another=x thing=x HTML my @array = $string =~ /(?:^| )(.+?)(?=(?: \w+=|$))/g; print "[$_]\n" for @array; # whoops, was "[$_\n]" # prints: [this=x] [that=x x] [another=x] [thing=x]

    Update: Corrected typo, per johngg below.

      I think your code would be more likely to print

      [this=x ][that=x x ][another=x ][thing=x ]

      Perhaps you meant

      print "[$_]\n" for @array;

      I hope this is helpful.

      Cheers,

      JohnGG

      I used \s* in place of (?:^| ) to make it work across blank lines.

      use strict; use warnings; my $string = <<HTML; this=x that=x x another=x thing=x van=x x x diesel=xx xx HTML my @array2 = $string =~ /\s*(.+?)(?=\s+\w+=|$)/g; print "[$_]\n" foreach (@array2);
      This helped me out, Thanks++.
Re: Split this string
by BrowserUk (Patriarch) on Oct 12, 2011 at 14:53 UTC

    print $string;; this=x that=x x another=x thing=x print "'$_'" for split m[ (?=\S+=)], $string;; 'this=x' 'that=x x' 'another=x' 'thing=x '

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Split this string
by raybies (Chaplain) on Oct 12, 2011 at 12:32 UTC
    One problem with converting to a pipe character is that now you've got to make sure that none of your symbols in your format have pipe character in it. So you've essentially added a third separator. The Pipe, the Equal sign, and the Space. That may not be a problem, but it's something to consider.
Re: Split this string
by thargas (Deacon) on Oct 12, 2011 at 12:59 UTC
    Is there a reason for not using the following?
    @array = split /\s/, $string, 2;
    This will only split on the first whitespace and will ignore any other whitespace. I.E. you'll only get at most two elements from the split.
      count the equals signs, that is how many pairs of data exist
Re: Split this string
by wwe (Friar) on Oct 12, 2011 at 12:37 UTC
    It all depends on your input. You should be aware about possible input you already mentioned whitespace but what is about metacharacters like brackets, qoutation marks, equal sign and so on? try to get data in a better format or at least check for all possible quirks you need to work around.
Re: Split this string
by Anonymous Monk on Oct 12, 2011 at 11:02 UTC

    to format the string the same way in maybe less lines.

    No, the format is adequately poor, ie it is inadequate

    Get a better format, something like JSON

      You must be an engineer: Your answer is completely valid but not useful to the question at hand, as it completely ignores that the OP may be stuck with the format that's been presented. Perhaps asking if the format is predefined or if this is something being developed would be a good place to start.

      Now, if the OP would kindly answer the "Is this something you're stuck with?" question, perhaps we can start helping.

        I am stuck with how the string is and what I ment by "to format the string the same way in maybe less lines." was less lines of perl code. Like optimizing split to do it all or a single cleverly crafted regular-expression.

        You must be an engineer...

        You're not a very good carnie. If the OP is stuck, OP will either explain so, or simply ignore my response

Re: Split this string
by TomDLux (Vicar) on Oct 14, 2011 at 14:55 UTC

    Split on equal signs. The first array element contains only the first key, 'this'. The next array element contains the value for 'this', followed by the next tag. Use rindex() to find the rightmost space character, and substr to partition. Everything before the space is the value for 'this', everything after that last space is the next value.

    You really should chomp the string.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.