G'day sidmuchrock,

I looks like you might be working with CSV data. If that's the case, Text::CSV already does what you want (if you also have Text::CSV_XS installed, it will run faster). Text::CSV also solves many other issues you might encounter with CSV data; e.g. fields containing the separator character, such as 800,900,"1,000". Unless you're doing this as a purely academic exercise, this really isn't a wheel you should be reinventing.

Here's a quick example. I used your posted data and added a bit more to show some features, including: just one, zero-length element; and the embedded separator character I showed above.

#!/usr/bin/env perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV::->new(); while (my $row = $csv->getline(*DATA)) { # Do something with all elements, e.g. print 'Elements: ', 0+@$row, '; Last: |', $row->[-1], "|\n"; } __DATA__ 1,,3,4,,, 1,,3,4,,,not_empty , 800,900,"1,000"

Output:

Elements: 7; Last: || Elements: 7; Last: |not_empty| Elements: 1; Last: || Elements: 2; Last: || Elements: 3; Last: |1,000|

— Ken


In reply to Re: But I want null values in my array by kcott
in thread But I want null values in my array by sidmuchrock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.