#!/usr/bin/perl use strict; use warnings; { my @names = ( [ 'John Doe Joe', 1 ], [ 'John D', 1 ], [ 'John ', 0 ], [ 'John', 0 ], ); foreach my $lr (@names) { my ( $name, $success ) = @{$lr}; my @result = split( /\s/, $name ); if ( ( @result > 1 && $success ) || ( @result == 1 && !$success ) ) { print "$name split correctly, list has " . ( scalar @result ) . " elements. - "; } else { print "$name split incorrectly, list has " . ( scalar @result ) . " elements. - "; } print join('|',@result) . "\n"; } } #### $ perl -w 1130518.pl John Doe Joe split correctly, list has 3 elements. - John|Doe|Joe John D split correctly, list has 2 elements. - John|D John split correctly, list has 1 elements. - John John split correctly, list has 1 elements. - John $