Re: Sorting and Writing Effecient Code
- From: xhoster@xxxxxxxxx
- Date: 02 Nov 2006 20:09:24 GMT
"banker123" <bradbrockman@xxxxxxxxx> wrote:
I have the code below that opens a directory, determines the current
date and extracts the file names that start with the current date, then
loops through the contents of those files to extract data in a scalar
context.
You seem to be mis-using the term "scalar context", in a way that
renders your question meaningless.
Question
1. How do I sort the variables in a scalar context not an array?
Should I be reading the variables I have extracted into an array?
If you want to sort them, then you need to store them. Either an array,
or maybe a hash.
2. This program executes every 15 seconds to output the contents of
the directory. The output is used to control work flow and meet crucial
deadlines. Is this effeciently written? Any suggestions?
You don't seem to be using strict. You should. As long as the program
does whatever it needs to do fast enough so that it is not the bottleneck,
it is hard to see how its efficiency would matter.
My first "production" perl program, after reading and learning perl for
about a month.
#Open directory
my $dir="G:/Formware/files/";
opendir DH, $dir or die "Cannot open$!";
You should probably use a lexical dir handle.
opendir my $dh, $dir or die $!;
#Read each file beginning with system date from directory
@files=grep(/^$date/,readdir(DH));
If the list can be big, you may not want to read everything into memory
at once.
while (defined (my $file=readdir($dh))) {
next unless $file =~ /^$date/;
Every time you execute this script, it reprocesses the same files it
already processed the last time. That is the fundamental inefficiency,
but how to fix it depends on external factors.
By the way, there was no indication anywhere in your code about what it is
you want to sort, or how you want it sorted.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.
- Follow-Ups:
- Re: Sorting and Writing Effecient Code
- From: banker123
- Re: Sorting and Writing Effecient Code
- References:
- Sorting and Writing Effecient Code
- From: banker123
- Sorting and Writing Effecient Code
- Prev by Date: FAQ 5.20 Why can't I just open(FH, ">file.lock")?
- Next by Date: Re: Masking/Hiding a password in Perl Source
- Previous by thread: Sorting and Writing Effecient Code
- Next by thread: Re: Sorting and Writing Effecient Code
- Index(es):
Relevant Pages
|