Hi,

I would like to parse the following line and store all the values into an array. The values are separated by commas. I am unsure of how to do this without having a super long parsing string(i.e if(/.*/). At first, I was thinking of some type of loop, but I don't know how to do that with the if(/.*/) statement.

I would like all the values separated by a comma be put into a 1 dimensional array. Can anyone help me? thanks.

|,0.005,0.004,0.004,0.005,0.006,0.005,0.006,0.005|,0.005,0.006,0.004,0.005,0.005,0.004,0.004,0.006|,0.004,0.004,0.004,0.004,0.004,0.005,0.004,0.004|,0.005,0.003,0.004,0.004,0.005,0.005,0.004,0.003|,0.005,0.003,0.004,0.004,0.005,0.004,0.005,0.004|,0.004,0.004,0.004,0.004,0.004,0.003,0.005,0.004|,0.005,0.004,0.005,0.005,0.005,0.005,0.005,0.004|,0.005,0.005,0.005,0.005,0.005,0.005,0.004,0.004

Dani AI

Generated

Short summary: the line contains numeric tokens separated by commas with pipe characters used as group boundaries. The simplest, most robust way to get a one‑dimensional array is to split on both delimiters, trim each token and drop empty entries. The idea from to handle the leading pipe is valid; the split+grep pattern used by is similar in effect but includes a whitespace split that may be unnecessary for this data.

A safe, clear workflow (flattened array):

  • Trim leading/trailing whitespace from the whole line.
  • Split on either comma or pipe.
  • Trim each piece, discard empty strings, optionally coerce to numeric.

Example implementation:

use warnings;
use strict;

my $line = ...;   # input line

# split on comma or pipe, trim each token and discard empties
my @parts = split /[|,]/, $line;
@parts = map { my $t = $_; $t =~ s/^\s+|\s+$//g; $t } @parts;
@parts = grep { length } @parts;
@parts = map { 0 + $_ } @parts;   # optional: force numeric context

If the pipe characters should preserve grouping, split on '|' first, then split each group on commas and collect sub-arrays:

my @groups = map {
  [ grep { length } map { my $e = $_; $e =~ s/^\s+|\s+$//g; $e } split /,/, $_ ]
} grep { length } split /\|/, $line;

Notes and troubleshooting:

  • Inside a character class ([|,]) the pipe need not be escaped; outside it must be written as |.
  • If fields can contain commas or pipes inside quoted values, use a proper CSV parser (Text::CSV or Text::CSV_XS) rather than simple split.
  • For very large files, process line-by-line and avoid building huge intermediate arrays.

Recommended Answers

All 2 Replies

#!/usr/bin/perl

$i = "|,0.005,0.004,0.004,0.005,0.006,0.005,0.006,0.005|,0.005,0.006,0.004,0.005,0.005,0.004,0.004,0.006|,0.004,0.004,0.004,0.004,0.004,0.005,0.004,0.004|,0.005,0.003,0.004,0.004,0.005,0.005,0.004,0.003|,0.005,0.003,0.004,0.004,0.005,0.004,0.005,0.004|,0.004,0.004,0.004,0.004,0.004,0.003,0.005,0.004|,0.005,0.004,0.005,0.005,0.005,0.005,0.005,0.004|,0.005,0.005,0.005,0.005,0.005,0.005,0.004,0.004";

# I have to get rid of the first '|,' otherwise the first
# element in the array will be empty.  If you don't mind
# then you can delete this line and just use the split
$i =~ s/^\|,//;

@values = split(/\|?,/,$i);
foreach $elm (@values){
   print "$elm\n";
}
#!/usr/bin/perl
use warnings;
use strict;

my $data =
'    |,0.005,0.004,0.004,0.005,0.006,0.005,0.006,0.005|,0.005,0.006,0.004,0.005,0.005,0.004,0.004,0.006|,0.004,0.004,0.004,0.004,0.004,0.005,0.004,0.004|,0.005,0.003,0.004,0.004,0.005,0.005,0.004,0.003|,0.005,0.003,0.004,0.004,0.005,0.004,0.005,0.004|,0.004,0.004,0.004,0.004,0.004,0.003,0.005,0.004|,0.005,0.004,0.005,0.005,0.005,0.005,0.005,0.004|,0.005,0.005,0.005,0.005,0.005,0.005,0.004,0.004';

my @data = grep $_,split /\s+?|\||,/, $data;  

print $_, $/ for @data;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.