split question
am 10.05.2003 17:18:32 von Vassiliy Truskov
Hello, everybody.
I use 'split' function to split my string into the array, but have a
problem.
In the script below I expect to have three elements in the array after
split proceed.
But it splits string on each character:
$strIn = "10.|345|test15";
$del = "|";
@array_out = split(/$del/, $strIn);
$i = 0;
foreach $elem (@array_out)
{
print "$i => $elem\n";
$i++;
}
If I set PATTERN without using the variable, it works. But the PATTERN
will change every time and I can't set the
value instead of variable.
Thanks in advance,
Vassiliy
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: split question
am 10.05.2003 18:34:39 von chengkai
Hi Vassiliy,
> $strIn = "10.|345|test15";
> $del = "|";
> @array_out = split(/$del/, $strIn);
> $i = 0;
> foreach $elem (@array_out)
> {
> print "$i => $elem\n";
> $i++;
> }
You will have to esacpe the | character, try this:
$del = '\|';
It should work!!
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: split question
am 11.05.2003 05:58:06 von David Weintraub
On Saturday, May 10, 2003, at 11:18 AM, Vassiliy Truskov wrote:
> Hello, everybody.
>
> I use 'split' function to split my string into the array, but have a
> problem.
> In the script below I expect to have three elements in the array after
> split proceed.
> But it splits string on each character:
>
> $strIn = "10.|345|test15";
> $del = "|";
> @array_out = split(/$del/, $strIn);
> $i = 0;
> foreach $elem (@array_out)
> {
> print "$i => $elem\n";
> $i++;
> }
>
> If I set PATTERN without using the variable, it works. But the PATTERN
> will change every time and I can't set the
> value instead of variable.
Your problem is that the pipe character is a regular expression
character, and you must quote it to prevent Perl from interpreting its
special meaning. You said that the split character will change each
time. I am assuming that you will be reading this in.
You can try quoting, but that can lead to strange problems. For
example, if you said this:
@array_out = split("\\$del", $strIn);
It will work except in cases where a combination of a backslash and
character is a special character like if $del = "n".
I originally thought that using quotes in the pattern specification
means that the pattern is a string and not a regular expression.
However, changing the line to "split("$del", $strIn);" seemed to do the
same thing. I even tried doing this:
eval qq{\@array_out = split('$del', $strIn)};
but got the same result. It appears that quotes, single quotes, and
slashes all mean the same thing in a split statement. (Is there a way
to "turn off" regular expression meaning in a split command?)
So, if you know that you won't be using separators like "n", "b", "r",
and a few other special letters, you can use quoting to get around the
problem. If your separators could include alphabetic characters, then
you might have to test for that before using the split command:
if ($del !~ /^[a-z]/)
{
$del = "\\$del";
}
@array_out = split("$del", $strIn);
--
David Weintraub _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
david@weintraubworld.net _/ _/
_/ I am the Great and Powerful Oz*_/
_/ _/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
(*Pay no attention to the man behind the curtains)
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: split question
am 11.05.2003 06:12:25 von Jason Waugh
On 10/05/2003 at 11:58 PM David Weintraub wrote:
>Your problem is that the pipe character is a regular expression
>character, and you must quote it to prevent Perl from interpreting its
>special meaning. You said that the split character will change each
>time. I am assuming that you will be reading this in.
....
>but got the same result. It appears that quotes, single quotes, and
>slashes all mean the same thing in a split statement. (Is there a way
>to "turn off" regular expression meaning in a split command?)
Yes, the statement in question should be this:
@array_out =3D split(/\Q$del\E/, $strIn);
the \Q and \E causes the regular expression to take what's between them=
literally.
this is the output I get:
0 =3D> 10.
1 =3D> 345
2 =3D> test15
with a program that looks like this:
use strict;
my $stringIn =3D "10.|345|test15";
my $delimiter =3D "|";
my @array_out =3D split(/\Q$delimiter\E/, $stringIn);
my $i =3D 0;
foreach my $element (@array_out)
{
print "$i =3D> $element\n";
$i++;
}
Jason W.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: split question
am 12.05.2003 16:01:29 von Vassiliy Truskov
Thanks a lot for your help!
Vassiliy
David Weintraub wrote:
>
> On Saturday, May 10, 2003, at 11:18 AM, Vassiliy Truskov wrote:
>
>> Hello, everybody.
>>
>> I use 'split' function to split my string into the array, but have a
>> problem.
>> In the script below I expect to have three elements in the array
>> after split proceed.
>> But it splits string on each character:
>>
>> $strIn = "10.|345|test15";
>> $del = "|";
>> @array_out = split(/$del/, $strIn);
>> $i = 0;
>> foreach $elem (@array_out)
>> {
>> print "$i => $elem\n";
>> $i++;
>> }
>>
>> If I set PATTERN without using the variable, it works. But the
>> PATTERN will change every time and I can't set the
>> value instead of variable.
>
>
> Your problem is that the pipe character is a regular expression
> character, and you must quote it to prevent Perl from interpreting its
> special meaning. You said that the split character will change each
> time. I am assuming that you will be reading this in.
>
> You can try quoting, but that can lead to strange problems. For
> example, if you said this:
>
> @array_out = split("\\$del", $strIn);
>
> It will work except in cases where a combination of a backslash and
> character is a special character like if $del = "n".
>
> I originally thought that using quotes in the pattern specification
> means that the pattern is a string and not a regular expression.
> However, changing the line to "split("$del", $strIn);" seemed to do
> the same thing. I even tried doing this:
>
> eval qq{\@array_out = split('$del', $strIn)};
>
> but got the same result. It appears that quotes, single quotes, and
> slashes all mean the same thing in a split statement. (Is there a way
> to "turn off" regular expression meaning in a split command?)
>
> So, if you know that you won't be using separators like "n", "b", "r",
> and a few other special letters, you can use quoting to get around the
> problem. If your separators could include alphabetic characters, then
> you might have to test for that before using the split command:
>
> if ($del !~ /^[a-z]/)
> {
> $del = "\\$del";
> }
>
> @array_out = split("$del", $strIn);
>
> --
> David Weintraub _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
> david@weintraubworld.net _/ _/
> _/ I am the Great and Powerful Oz*_/
> _/ _/
> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
> (*Pay no attention to the man behind the curtains)
>
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: split question
am 13.05.2003 01:12:42 von Vassiliy Truskov
David,
In this example 'split' splits a string into 5 elements array missing
last one because there is no any values.
$strIn = "000000708|10101|20011231|0|0||";
$del = "|";
@array_out = split(/\Q$del\E/, $a);
print join(':',@array_out);
Is there a way to replace the last element (which is missed after split )
to \s while I split this string or I should prepared string by replacing
all missed values prior to send it to split.
Thanks in advance,
Vassiliy
David Weintraub wrote:
>
> On Saturday, May 10, 2003, at 11:18 AM, Vassiliy Truskov wrote:
>
>> Hello, everybody.
>>
>> I use 'split' function to split my string into the array, but have a
>> problem.
>> In the script below I expect to have three elements in the array
>> after split proceed.
>> But it splits string on each character:
>>
>> $strIn = "10.|345|test15";
>> $del = "|";
>> @array_out = split(/$del/, $strIn);
>> $i = 0;
>> foreach $elem (@array_out)
>> {
>> print "$i => $elem\n";
>> $i++;
>> }
>>
>> If I set PATTERN without using the variable, it works. But the
>> PATTERN will change every time and I can't set the
>> value instead of variable.
>
>
> Your problem is that the pipe character is a regular expression
> character, and you must quote it to prevent Perl from interpreting its
> special meaning. You said that the split character will change each
> time. I am assuming that you will be reading this in.
>
> You can try quoting, but that can lead to strange problems. For
> example, if you said this:
>
> @array_out = split("\\$del", $strIn);
>
> It will work except in cases where a combination of a backslash and
> character is a special character like if $del = "n".
>
> I originally thought that using quotes in the pattern specification
> means that the pattern is a string and not a regular expression.
> However, changing the line to "split("$del", $strIn);" seemed to do
> the same thing. I even tried doing this:
>
> eval qq{\@array_out = split('$del', $strIn)};
>
> but got the same result. It appears that quotes, single quotes, and
> slashes all mean the same thing in a split statement. (Is there a way
> to "turn off" regular expression meaning in a split command?)
>
> So, if you know that you won't be using separators like "n", "b", "r",
> and a few other special letters, you can use quoting to get around the
> problem. If your separators could include alphabetic characters, then
> you might have to test for that before using the split command:
>
> if ($del !~ /^[a-z]/)
> {
> $del = "\\$del";
> }
>
> @array_out = split("$del", $strIn);
>
> --
> David Weintraub _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
> david@weintraubworld.net _/ _/
> _/ I am the Great and Powerful Oz*_/
> _/ _/
> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
> (*Pay no attention to the man behind the curtains)
>
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: split question
am 13.05.2003 02:16:52 von dbecoll
Vassiliy Truskov wrote:
> David,
>
> In this example 'split' splits a string into 5 elements array missing
> last one because there is no any values.
>
> $strIn = "000000708|10101|20011231|0|0||";
> $del = "|";
> @array_out = split(/\Q$del\E/, $a);
> print join(':',@array_out);
>
> Is there a way to replace the last element (which is missed after split )
> to \s while I split this string or I should prepared string by replacing
> all missed values prior to send it to split.
Just add a number to the end that is >= to your max number of args:
my @array_out = split /\Q$del\E/, $StrIn, 10;
--
,-/- __ _ _ $Bill Luebkert Mailto:dbecoll@adelphia.net
(_/ / ) // // DBE Collectibles Mailto:dbe@todbe.com
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re : split question
am 19.09.2007 16:15:54 von stephanelegault
Thanks, it what I wanted!
----- Message initial ----
De : Paul Gowers
=C0 : "Thurn, Martin"
Cc : Stephane Legault ; activeperl@listserv.Acti=
veState.com
Envoy=E9 le : mercredi 19 septembre 2007, 10 h 14 min 48 s
Objet : Re: split question
I think it can:
my ($data) =3D "sample1:sample2:sample3:sample4";
my ($var1, $var2) =3D split (/:/, $data, 2);
print "var1 is $var1\nvar2 is $var2\n";
perl test.pl
var1 is sample1
var2 is sample2:sample3:sample4
perldoc -f split
Paul
On 19 Sep 2007, at 14:26, Thurn, Martin wrote:
> Split can not do that. Use a regex such as
>
> my $sList =3D 'sample1:sample2:sample3:sample4:sample5';
> my ($s1, $s2) =3D ($sList =3D~ m!\A([^:]*):(.+)\Z!);
> print "s1=3D$s1=3D s2=3D$s2=3D\n";
>
>
> - - Martin
>
> -----Original Message-----
> From: activeperl-bounces@listserv.ActiveState.com
> [mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
> Stephane Legault
> Sent: Wednesday, September 19, 2007 09:16
> To: activeperl@listserv.ActiveState.com
> Subject: split question
>
> Hi, I want split a list in two parts from the same delimiter =
> character.
>
> Here is the list [sample1:sample2:sample3:sample4:sample5]
>
> I would like a $variable containing sample1 and another one containing
> the rest.
>
> I search a lot on Internet and find how to split this in many variable
> ($x,$y)=3Dsplit(/:/) but this is not useful for me.
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Obtenez des r=E9ponses =E0 vos questions ! =
Profitez des connaissances et des opinions des internautes sur Yahoo! Quest=
ions/R=E9ponses
http://fr.rd.yahoo.com/evt=3D42054/*http://qc.answers.yahoo. com
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs