http://www.perlmonks.org?node_id=1013764


in reply to Need help in parsing an input

Hello sandeepda, and welcome to the Monastery!

If I’ve understood your question correctly, the following script should give you an idea of how to proceed:

#! perl use Modern::Perl; my %ports; while (<DATA>) { next unless /^http/; my @records = split /\s+/; if (exists $ports{$records[2]}) { warn "Attempt to give port $records[2] a second name"; } else { $ports{$records[2]} = $records[0]; } } say join(' and ', get_listener_name($_)) for 6712, 4516; sub get_listener_name { my ($port_to_be_checked) = @_; my @results = ('FALSE', 'a1=null'); if (exists $ports{$port_to_be_checked}) { @results = ('TRUE', 'a1=' . $ports{$port_to_be_checked} . '(li +stener name)'); } return @results; } __DATA__ name ip port ------------------------------------ http-listener-1 * 6712 http-listener-2 * 8709

Output:

21:40 >perl 486_SoPW.pl TRUE and a1=http-listener-1(listener name) FALSE and a1=null 21:40 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,