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. | [reply] [d/l] |
|
|
[this=x
][that=x x
][another=x
][thing=x
]
Perhaps you meant
print "[$_]\n" for @array;
I hope this is helpful.
| [reply] [d/l] [select] |
|
|
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);
| [reply] [d/l] [select] |
|
|
This helped me out, Thanks++.
| [reply] |
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.
| [reply] [d/l] |
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. | [reply] |
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. | [reply] [d/l] |
|
|
count the equals signs, that is how many pairs of data exist
| [reply] |
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. | [reply] |
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
| [reply] |
|
|
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.
| [reply] |
|
|
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.
| [reply] |
|
|
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
| [reply] |
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.
| [reply] |