use strict; use warnings; while( my $str = ) { chomp $str; print "\nsplitting $str with these LIMITs:\n"; foreach my $limit ( 0, 5, 7, -1 ) { my $result = munge( $str, $limit ); printf " %2d => [$result]\n", $limit; } } sub munge { my ( $str, $limit ) = @_; my @fields = split( '\|', $str, $limit ); @fields = map { $_ || 'empty' } @fields; # using a different delimiter to illustrate non-split fields return join( '!', @fields ); } __DATA__ 1|2|3|4|5|6 1|2||4|| |2|3||| ||||| #### splitting 1|2|3|4|5|6 with these LIMITs: 0 => [1!2!3!4!5!6] 5 => [1!2!3!4!5|6] 7 => [1!2!3!4!5!6] -1 => [1!2!3!4!5!6] splitting 1|2||4|| with these LIMITs: 0 => [1!2!empty!4] 5 => [1!2!empty!4!|] 7 => [1!2!empty!4!empty!empty] -1 => [1!2!empty!4!empty!empty] splitting |2|3||| with these LIMITs: 0 => [empty!2!3] 5 => [empty!2!3!empty!|] 7 => [empty!2!3!empty!empty!empty] -1 => [empty!2!3!empty!empty!empty] splitting ||||| with these LIMITs: 0 => [] 5 => [empty!empty!empty!empty!|] 7 => [empty!empty!empty!empty!empty!empty] -1 => [empty!empty!empty!empty!empty!empty]