in reply to splinting a line of text by comma
This appears to work given the data you provided. However, it does not handle multiple fields that contain commas. It also does not handle quoted commas and the like. You would need to use Text::CSV or similar for that.
Adapt the outer loop as needed (e.g. if reading from a file handle):
use 5.010; my @lines = ('some text FOO | some text BAR | oh , no , commas | some text BAZ + | some text QUX'); for my $line (@lines) { chomp $line; if ($line =~ /,/) { my @items = split /,/, $line; my $prefix = substr $items[0], 0, rindex $items[0], '|'; my $suffix = substr $items[-1], index $items[-1], '|'; #say $prefix; #say $suffix; foreach my $item (@items) { say "$prefix | $item $suffix"; } } else { say $line; } }
|
|---|