Perl Separation Function - Sequential Separators

I use the split function to split each record in a file. Say the $ delimiter.

 my @fields = split(/\$/,$record); 

If each record has 4 fields, and if some fields are empty, there are two consecutive dollar symbols, for example:

 abc$efg$ehd$rty abc$$$ 

split does not work for the second record, since after the split there are only 2 fields instead of 4.

Any idea how to fix this, or if there are better options?

+4
source share
1 answer

From the split documentation :

If the LIMIT is negative, it is treated as if an arbitrarily large LIMIT was given.

What does this mean if you do something like:

 my @fields = split( /\$/, $record, -1 ); 

... then you will get blank fields for the last three entries in the list.

 #!perl use strict; use warnings; use Data::Dumper; my $string = 'abc$$$'; my @fields = split( /\$/, $string, -1 ); print Dumper \@fields; 

Fingerprints:

 $VAR1 = [ 'abc', '', '', '' ]; 
+11
source

Source: https://habr.com/ru/post/1336862/


All Articles