I came upon the following, which seems to be in line with spec, but I think is inconsistent.

I write a hash to a file delimited by tabs, eg

my $fn=open('data.csv',:w);
my %x=<one two three four> Z 1,2,2.1,3;
$fn.say('record-name'~map("\t$^a\t$^b"),%x);
$fn.close;

The output sometimes contains either the keys or the values padded with single spaces on either side (I cannot find a pattern for the spaces). I am not sure whether this a bug of say (or $fn.say() ).

So now I want to read in the data again. (The problem arises because perl6 has a memory leak and my program segfaults after five loops, so I need to store the intermediate data; this is a serialisation problem, but I dont need to go to the extreme of yaml for it).

my $fn=open('data.csv',:r);
my $record-name;
my @fields;
($record-name,@fields)=$fn.lines(1).split(/\t/);
my %x = %(map{$^a => $^b}, @fields);

Except that the spaces added by .say are included in the key/value parts. This screws up the program as spaces are not expected.

The spec specifically says that split will not trim the results. Hence, the solution is
($record-name,@fields)=map {.trim}, $fn.lines(1).split(/\t/);

This does seem to be a bit inconsistent in that I am outputting and inputting with essentially the same sort of scripting, but I am not getting a predictable end result.

The spec also seems to recommend .comb

I have a problem with this because some of the data I want to read in has empty cells, eg.
datum1\tdatum2\t\tend-datum

I want this to end up in an array
['datum1','datum2',*,'end-datum']

Yet I cant see how .comb will accomplish this. Could someone enlighten me?

But even if it was not a problem with .say, I could see that white space trimming would be useful with split.

So, is there some good reason why .split does not automatically trim?

Richard

Reply via email to