Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: But I want null values in my array

by kcott (Bishop)
on Mar 11, 2020 at 19:51 UTC ( #11114144=note: print w/replies, xml ) Need Help??


in reply to But I want null values in my array

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

Replies are listed 'Best First'.
Re^2: But I want null values in my array
by Anonymous Monk on Mar 11, 2020 at 20:04 UTC
    Don't forget blank_is_undef

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11114144]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2020-06-01 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (5 votes). Check out past polls.

    Notices?