Re: Sorting and Writing Effecient Code



"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
.



Relevant Pages

  • Re: How would I do the following?
    ... this only works if "expression" returns an array. ... will be returned in scalar context. ... In the past, though, I have been burned by shoddy documentation ...
    (comp.lang.perl.misc)
  • Re: a simple question
    ... Have you seen the Posting Guidelines that are posted here frequently? ... Here the name of the array is in *scalar* context. ...
    (comp.lang.perl.misc)
  • Re: array question (newbie)
    ... The reason for this is that $sizeis a scalar value, ... context is imposed on the array @foo. ... Assigning to a list imposes list context. ...
    (comp.lang.perl.misc)
  • Re: Question about minloc
    ... in the context that you are using minloc. ... Because m is a scalar, ... the distinction between a scalar and an array of size 1, ... In that context, either an array or a scalar is ok.... ...
    (comp.lang.fortran)
  • Re: About Lagrange in GR II
    ... Yes, but remember that's the classical Lagrangian, not the Lagrangian density of GR. ... local and coordinate dependant unit, ... The action itself is a scalar. ... I suspect you are confusing the context of GR with the context of Quantum Field Theory in which there are often "running coupling constants". ...
    (sci.physics.relativity)