#!/usr/bin/perl use strict; use warnings; my @selection = qw/User1 User4/; my $reg = join '|',@selection; my @array = ( 'User1 PS1 10.10.10.1', 'User2 PS2 10.10.10.2', 'User3 PS3 10.10.10.3', 'User4 PS4 10.10.10.4', ); my @ip = map { (split /\s+/)[-1] } grep(/^(?:$reg)/,@array); local $, = "|\n"; print @ip;