#!/usr/local/bin/perl use strict; use warnings; my @source = ("John Ronald Reuel Tolkien","John Ronald S Tolkien","Trent Reznor","Barack Hussein Obama II","Barack Hussein II"); my @search = ("John Ronald Reuel T","Trent Reznor","Barack Hussein II","Barack Hussein Obama II","No match here"); print "twonames\t\talternativesearch\n"; foreach my $s (@search){ #gets first two names (my $twonames=$s)=~s/^(\w+ \w+).*$/$1/; #gets all other names, if they exist (my $others=$s)=~s/^(\w+ \w+)//; if ($others){ #deletes initial space (my $alternativesearch=$others)=~s/^\s//; $alternativesearch=~s/\s/\|/g; print "$twonames\t\t$alternativesearch\n"; } else { print "$twonames\t\tNO OTHER NAMES PRESENT\n"; } } #prints # twonames alternativesearch # John Ronald Reuel|T # Trent Reznor NO OTHER NAMES PRESENT # Barack Hussein II # Barack Hussein Obama|II