Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Bolt on where a match is not found to a print script

by hippo (Bishop)
on Dec 05, 2017 at 11:30 UTC ( [id://1204945]=note: print w/replies, xml ) Need Help??


in reply to Bolt on where a match is not found to a print script

Keep a tally:

#!/perl/bin/perl use strict; use warnings; my @files = <c:/perl64/myfiles/*>; my $total = 0; foreach my $file ( @files ) { open my $file_h, '<', $file or die "Can't open $file: $!"; while ( <$file_h> ) { if (/\b120[347]\b/) { print "$file $_"; $total++; } } } print "No files matched.\n" unless $total;

(Edited for typos)

Replies are listed 'Best First'.
Re^2: Bolt on where a match is not found to a print script
by QM (Parson) on Dec 05, 2017 at 11:46 UTC
    I think they wanted to check for each number separately. But this is most of the way there -- I'd just add a hash for the match, something like $matched{$1} = 1, and check that at the end.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Hmmm. Yes, you could well be right about each one requiring a separate flag, although that's not how I originally read the requirement. At least our anonymous brother has 2 marginally differing approaches to choose between. The whole thing does have something of an XY ring to it. Perhaps we'll find out.

        Thank you so much guys.. so the primary function of the code is to search through the text files in the folder and print every line that includes a the exact match to the number (contained within the line). the secondary requirement is that IF after searching through all the files no single match is found of the number then print at the bottom that it was not matched

        this is a sample of the data:

        Port Name Status Vlan Duplex Speed Fa0/1 Sohnsons connected 1404 full 100 Fa0/2 Sohnsons connected 1404 full 100 Fa0/3 XYZ connected 1221 full 100 Fa0/4 Sohnsons notconnect 1404 full 100

        If you searched the files for 3 number (1404,1221,1234) 1234 would not be matched and printed, thus we would want "1234 not matched" printed. The code you presented worked really well for a single search, but we need to search multiple values

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-16 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found