Well, if you want to keep the stuff you split on, use capturing parentheses. If you don't want to keep the stuff, don't use them. $1 is never set, so the way you are trying it will never work.
| [reply] |
First, as you see, from my example, capturing parenthesis do not work, i.e., the string is not splitted according to what I intended to be a separator: 'abc'.
Second, you said "if you want to keep the stuff you split on"... Yes, this is what I want, but,
please don't tell me that "the way you are trying it will never work". Of course, I know that, and this is the reason I'm posting a question here.
By the way, I'm new here, and I understood this is the site to ask for some help, if it isn't, please tell me where to go with my silly questions.
| [reply] |
Actually, the string was split according to the separator. But you put the separator into parentheses, which also put the separator into the result list. Maybe it is just the way that you are printing your results that confuses you?
#!perl -w
use strict;
use Data::Dumper;
my @a1 = split /abc/, "uno abc dos";
my @a2 = split /(abc)/, "uno abc dos";
print "a1: ", Dumper \@a1;
print "a2: ", Dumper \@a2;
__END__
a1: $VAR1 = [
'uno ',
' dos'
];
a2: $VAR1 = [
'uno ',
'abc',
' dos'
];
These two variants of results are what you can get using a plain split. Others have already replied with ways you can modify those results to move, for example, the second element of the array into $1, but split does not set or modify the capture variables. | [reply] [d/l] [select] |
First, as you see, from my example, capturing parenthesis do not work, i.e., the string is not splitted according to what I intended to be a separator: 'abc'.
Um, no
#!/usr/bin/perl --
use strict; use warnings;
use Data::Dumper;
my @a1 = split /abc/, "uno abc dos";
my @a2 = split /(abc)/, "uno abc dos";
my @a3 = split /(?:abc)/, "uno abc dos";
print "a1 @a1\n", Dumper( \@a1 ), "\n";
print "a2 @a2\n", Dumper( \@a2 ), "\n";
print "a3 @a3\n", Dumper( \@a3 ), "\n";
__END__
a1 uno dos
$VAR1 = [
'uno ',
' dos'
];
a2 uno abc dos
$VAR1 = [
'uno ',
'abc',
' dos'
];
a3 uno dos
$VAR1 = [
'uno ',
' dos'
];
Tutorials: Debugging and Optimization: Basic debugging checklist
By the way, I'm new here, and I understood this is the site to ask for some help, if it isn't, please tell me where to go with my silly questions.
Welcome, see The Perl Monks Guide to the Monastery | [reply] [d/l] |
This doesn't solve my problem, because the matched string isn't stored in $1 ...
You were saying?
@a3 = split /(?:abc)/, "uno abc dos";
print "@a3\n";
__END__
uno dos
| [reply] [d/l] |