Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Help with a faster loop

by JediWizard (Deacon)
on Mar 01, 2006 at 14:44 UTC ( #533656=note: print w/replies, xml ) Need Help??

in reply to Help with a faster loop

if ($file_path =~ /defines|sccpch|sms81154|sms97767/) { next; } elsif(!($file_path =~ /defines|sccpch|sms81154|sms97767/)) # This + is redunant { if (open(FILES, $file_path)) # use +the -f operator to test # file +s existance, or -W to test # if i +t can be written to { if (!($file_path =~ /\.lfa|\.zip|\.txt|UASTG/)) { print (DATA "$file_path\n"); } } else { die ("Could not open[$file_path], $!\n"); } ## The above is redunant... it would be better stated as: if ($file_path =~ /defines|sccpch|sms81154|sms97767/) { next; } else { if (-f $file_path) { if (!($file_path =~ /\.lfa|\.zip|\.txt|UASTG/)) { print (DATA "$file_path\n"); } } else { die ("Could not open[$file_path], $!\n"); } }

They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^2: Help with a faster loop
by CountOrlok (Friar) on Mar 01, 2006 at 16:00 UTC
    I think the code below looks cleaner. Depending on what your code does, you can use the memoize module to keep a cache of files you have already checked. Passing $dir_path and $filepath to the subroutine is definitely a good idea. My point here is that it is best to keep subroutines self-contained and not use external variables:
    use strict; use Memoize; memoize ('read_files'); sub process_files { my $dir_path = shift; if (opendir(TEST, $dir_path)) { my @files = sort grep{$_ ne '.' and $_ ne '..'} readdir(TEST); #print "\n@files[0]\n"; read_files("$dir_path\\$_") foreach (@files); } else { die ("Could not Opendir $dir_path: $!\n"); }closedir TEST; } sub read_files { my $file_path = shift; if (-f $file_path) { print (DATA "$file_path\n") if ($file_path !~ /(\.lfa|\.zip|\. +txt|UASTG)$/); } else { die ("Could not open[$file_path], $!\n"); } }
    The only global here is the DATA filehandle, but you should pass that along through the subroutine chain as a parameter.
      Thanks a lot for your assistance.

      I will be opening various xml files in som of my files, without using xml parser module, is it possible to get information from tags.

      Example: if a xml file has a tag <edman> Hello World! </edman> within it, and I want to look for "Hello" within any files, how do i read b/w tags.

Re^2: Help with a faster loop
by gzayzay (Sexton) on Mar 01, 2006 at 15:06 UTC
    Thanks Andy, I used your modification and it is working. Edman

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (None)
    As of 2022-01-26 06:19 GMT
    Find Nodes?
      Voting Booth?
      In 2022, my preferred method to securely store passwords is:

      Results (69 votes). Check out past polls.