Re: Difficulties with split
by almut (Canon) on Apr 27, 2009 at 23:06 UTC
|
If I'm understanding you correctly, you want
@vals = unpack "(A2)*", $in;
| [reply] [d/l] |
|
|
That doesn't work with strings internally encoded as UTF8 before 5.10.0 (and not at all before 5.8.0).
If you're working with arbitrary characters, if you don't to mess with utf8::downgrade, or if you are worried about backwards compatibility, you can use the following instead:
@vals = $in =~ /(..)/sg;
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: Difficulties with split
by runrig (Abbot) on Apr 27, 2009 at 23:08 UTC
|
Think about it. You are splitting ON any two characters. If you were splitting on, say, a comma (","), would you expect commas to end up in the result? Some options, one, use a zero-width look behind assertion: split /(?<=..)/, $in; # johngg is right...bad answer
or capture what you're splitting on, and filter out empty elements:grep { length($_) > 0 } split /(..)/, $in;
or do like almut says above and use unpack :-)
| [reply] [d/l] [select] |
|
|
$ perl -le '
-> $str = q{abcdefg};
-> @bits = split m{(?<=..)}, $str;
-> print for @bits;'
ab
c
d
e
f
g
$
It seems that split is not perhaps the handiest tool for this task.
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: Difficulties with split
by jwkrahn (Abbot) on Apr 28, 2009 at 00:43 UTC
|
my @vals = $ARGV[ 0 ] =~ /../g;
print @vals;
| [reply] [d/l] |
|
|
Don't forget about the last char in strings of odd length:
my @vals = $ARGV[ 0 ] =~ /..|.$/g;
print @vals;
| [reply] [d/l] |
|
|
my @vals = $ARGV[ 0 ] =~ /..?/g;
print @vals;
| [reply] [d/l] |
Re: Difficulties with split
by perliff (Monk) on Apr 28, 2009 at 09:04 UTC
|
#!/usr/bin/perl
use warnings;
$in = $ARGV[0];
@vals = split(/\.\./, $in);
print @vals;
perliff
----------------------
"with perl on my side"
| [reply] [d/l] |
|
|
A full stop is a regular expression metacharacter meaning match any character (with caveats, see perlretut, perlre). However, a backslash escapes the special meaning of a metacharacter so that \. matches a literal full stop. That means you are spliting on two literal full stops, which is not quite what was wanted.
$ perl -le '
> $in = q{abcdefg};
> @vals = split m{\.\.}, $in;
> print for @vals;'
abcdefg
$ perl -le '
> $in = q{abc..defg};
> @vals = split m{\.\.}, $in;
> print for @vals;'
abc
defg
$
I hope this is of interest.
| [reply] [d/l] [select] |