Split a string in perl

J

John

There must be something that I have ignored in PERL.

To split a string with delimiter "|", sometimes, I need to give "\",
sometimes it works for "\\" only.

Here are the details.

The String is " 0000|Null".

And the ways I have tried to do split are:
1. my @columns = split('\|', $String);
2. my ( $proj, $desc) = split('\|', $String);
3. my ( $proj, $desc) = split('\\|', $String);

Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
receive an empty string .

What was wrong with case 2 , I thought if Case 1 works, Case2 should
be working also, Right ? I am using perl 5.6.1 on linux box.



Thanks a lot for the tips.


John.
 
C

comp.llang.perl.moderated

There must be something that I have ignored in PERL.

To split a string with delimiter "|", sometimes, I need to give "\",
sometimes it works for "\\" only.

Here are the details.

The String is " 0000|Null".

And the ways I have tried to do split are:
1. my @columns = split('\|', $String);
2. my ( $proj, $desc) = split('\|', $String);
3. my ( $proj, $desc) = split('\\|', $String);

Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
receive an empty string .

What was wrong with case 2 , I thought if Case 1 works, Case2 should
be working also, Right ? I am using perl 5.6.1 on linux box.

case 2 works as is. Is it possible you meant:
split( "\|", $String ) ...?

In that case, double-quotish interpolation causes
your split regex to be just '|':

# perl -MO=Deparse,-p -e 'split "\|"," 0000|Null"'
split(/|/, ' 0000|Null', 0);
-e syntax OK

Then, you have the alternation meta character
alone with empty alternatives on either side
which'll match the 1st 2 characters encountered:

$proj=' ' $desc='0'

Is it possible you had 2 leading blanks in
string which "looked" like 2 empty strings for
$proj and $desc...
 
B

Ben Morrow

Quoth John said:
There must be something that I have ignored in PERL.

To split a string with delimiter "|", sometimes, I need to give "\",
sometimes it works for "\\" only.

Here are the details.

The String is " 0000|Null".

And the ways I have tried to do split are:
1. my @columns = split('\|', $String);
2. my ( $proj, $desc) = split('\|', $String);

The first argument to split is a regex (with the special exception of
' '). Don't pass something that looks like a string, you'll just confuse
yourself.

my (@proj, $desc) = split(/\|/, $String);

Ben
 
J

John

case 2 works as is.  Is it possible you meant:
split( "\|", $String ) ...?

In that case, double-quotish interpolation causes
your split regex to be just '|':

# perl -MO=Deparse,-p -e 'split "\|"," 0000|Null"'
split(/|/, ' 0000|Null', 0);
-e syntax OK

Then, you have the alternation meta character
alone with empty alternatives on either side
which'll match the 1st 2 characters encountered:

  $proj=' '  $desc='0'

Is it possible you had 2 leading blanks in
string which "looked" like 2 empty strings for
$proj and $desc...

I do not have any leading blanks in this String.

-John
 
J

Jürgen Exner

John said:
There must be something that I have ignored in PERL.

To split a string with delimiter "|", sometimes, I need to give "\",
sometimes it works for "\\" only.

Here are the details.

The String is " 0000|Null".

And the ways I have tried to do split are:
1. my @columns = split('\|', $String);
2. my ( $proj, $desc) = split('\|', $String);
3. my ( $proj, $desc) = split('\\|', $String);

Case 1 and Case 3 are good. But Case 2 failed, both $proj and $desc
receive an empty string .

I cannot reproduce your results:

C:\tmp>type t.pl
use strict; use warnings;
my $String = " 0000|Null";
my @columns = split('\|', $String);
print "@columns\n";
my ( $proj, $desc) = split('\|', $String);
print $proj, ' ', $desc, "\n";
( $proj, $desc) = split('\\|', $String);
print $proj, ' ', $desc , "\n";

C:\tmp>t.pl
0000 Null
0000 Null
0000 Null

Each of versions produces the same result as the others.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top