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

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.