Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Split a string in perl

Reply
Thread Tools

Split a string in perl

 
 
John
Guest
Posts: n/a
 
      02-22-2008


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.





 
Reply With Quote
 
 
 
 
comp.llang.perl.moderated
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 11:01 am, John <chunj...@gmail.com> wrote:
> 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...

--
Charles DeRykus
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      02-22-2008

Quoth 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);


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

 
Reply With Quote
 
John
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 11:56*am, "comp.llang.perl.moderated" <c...@blv-
sam-01.ca.boeing.com> wrote:
> On Feb 22, 11:01 am, John <chunj...@gmail.com> wrote:
>
>
>
>
>
> > 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...
>
> --
> Charles DeRykus- Hide quoted text -
>
> - Show quoted text -


I do not have any leading blanks in this String.

-John
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-22-2008
John <> wrote:
>
>
>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
 
Reply With Quote
 
comp.llang.perl.moderated
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 12:34 pm, John <chunj...@gmail.com> wrote:
> On Feb 22, 11:56 am, "comp.llang.perl.moderated" <c...@blv-
>
>
>
> sam-01.ca.boeing.com> wrote:
> > On Feb 22, 11:01 am, John <chunj...@gmail.com> wrote:

>
> > > 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...

>
> > --
> > Charles DeRykus- Hide quoted text -

>
> > - Show quoted text -

>
> I do not have any leading blanks in this String.
>


Hm, the original post does however:

> "The String is " 0000|Null".


--
Charles DeRykus

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments