If you don't specify a LIMIT argument to split(), it chops off trailing null
values, so your last few fields aren't listed.

If you pass a negative argument, though, it'll pass those trailing nulls. So
what you want is:
<CODE>
 my @asLine = split (/,/, $_, -1);
</CODE>

Here's my code:
<CODE>
$string =
'Broken,Heart,36,20,05,05/03/2000,F,6,5,4,5,5,8,5,5,5,48,06,12,01,,,';
my @fields = split(/,/, $string, -1);
for (0 ... $#fields) {
    print "$_: '$fields[$_]'\n";
}
</CODE>

Here's my output:

0: 'Broken'
1: 'Heart'
2: '36'
3: '20'
4: '05'
5: '05/03/2000'
6: 'F'
7: '6'
8: '5'
9: '4'
10: '5'
11: '5'
12: '8'
13: '5'
14: '5'
15: '5'
16: '48'
17: '06'
18: '12'
19: '01'
20: ''
21: ''
22: ''

(Remember, arrays start at index 0...)


> -----Original Message-----
> From: Brian Bukeavich [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, June 28, 2001 1:35 PM
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: Re: split function question
>
>
> Input data looks like:
> Broken,Heart,36,20,05,05/03/2000,F,6,5,4,5,5,8,5,5,5,48,06,12,01,,,
>
> I am expecting:
> Field 1 = Broken
> Field 2 = Heart
> Field 3 = 36
> Field 4 = 20
> Field 5 = 05
> Field 6 = 05/03/2000
> Field 20 = ""
> Field 21 = ""
> Field 22 = ""
> ----Original Message Follows----
> From: Curtis Poe <[EMAIL PROTECTED]>
> Reply-To: [EMAIL PROTECTED]
> To: CGI Beginners <[EMAIL PROTECTED]>
> Subject: Re: split function question
> Date: Thu, 28 Jun 2001 12:55:20 -0700 (PDT)
>
> --- Brian Bukeavich <[EMAIL PROTECTED]> wrote:
>  > I need a little help with the split function. I'm trying to
> split a line
>  > based on comma delimeters(,), but when I use the syntax below
> I don't get
>  > the results I expect.  What am I doing wrong?  Is there a an special
> escape
>  > sequence for a comma?
>  >
>  > my @asLine = split (/,/, $_);
>
> Just a quick guess, but much of the comma-delimited data that I see has
> quoted fields that have
> commas embedded in the quotes:
>
>    "Poe, Curtis", 34, "Aspiring Screenwriter, Programmer"
>
> Trying to split the above line on commas will result in five
> fields instead
> of three.  You should
> probably use a module like Text::CSV or something similar.
>
> If you show us some sample input, expected output, and actual
> output, we can
> offer better advice.
>
> Cheers,
> Curtis Poe
>
> =====
> Senior Programmer
> Onsite! Technology (http://www.onsitetech.com/)
> "Ovid" on http://www.perlmonks.org/
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com
>
>

Reply via email to