Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: compare array with file

by trizen (Hermit)
on Apr 24, 2012 at 12:27 UTC ( #966815=note: print w/ replies, xml ) Need Help??


in reply to compare array with file

use strict; use warnings; my @c; my @a = qw ( server1 server2 server3 server5 ); my %a = map { $_ => 1 } @a; # Solution 4 my $filename = 'file'; open my $fh, '<:crlf', $filename or die "$0: Can't open $filename $!"; while (defined(my $line = <$fh>)) { chomp $line; # Solution 1 push @c, $line if not $line ~~ \@a; # Solution 2 push @c, $line if not grep { $line eq $_ } @a; # Solution 3 use List::Util qw(first); push @c, $line if not first { $_ eq $line } @a; # Solution 4 push @c, $line if not exists $a{$line}; } close $fh; print "@c";


Comment on Re: compare array with file
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://966815]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2015-07-01 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (22 votes), past polls