Dear Masters,
I have a script which basically does the following:
- Accept an input file text
- Check whether the exact text appear in one of the
file in the repository.
We have a hundreds of files in store.
- Return the name of the text in the repository
if we have it, otherwise
- Return 'no matching file'
For example we would then have:
prompt> perl check_filename.pl input_file.txt
prompt> Files in our repos: file1.txt
I was thinking if there is an efficient way to do
this sort of thing on the fly? What I have now
is simply storing them in hash. But it is very2 slow.
#!/usr/bin/perl -w
use strict;
use File::Slurp;
use File::Basename;
my $input = $ARGV[0];
my @repos = glob("repos/*.txt");
# Store repos in hash
my %hash;
foreach my $repos_file (@repos) {
my $text = read_file($repos_file);
my $base = basename($repos_file,".txt");
$hash{$text} = $base;
}
# check input files:
my $input_text = read_file($input);
if ($hash{$input_text}) {
print "File in our repos: $hash{$input_text}\.txt\n";
}
else {
print "no matching file\n";
}
---
neversaint and everlastingly indebted.......
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.