.------[ Duarte Cordeiro wrote (2002/10/14 at 16:42:35) ]------
 | 
 |  Hi,
 |   
 |   Background:
 |  I have a @filelist that can have (guess what) several filenames.
 |  I want to split the filenames in 3 arrays:
 |   - if the file is in a directory *\templatedata\*\data\ it should go 
 |     to array1;
 |   - if the file is in a directory \binaries\* it should go to array2;
 |   - any other file should go to array3;
 |   
 |  My program is as follows, but I think someone around can make the same 
 |  thing in 4 ir 5 lines :)
 |  Several questions:
 |   
 |  1) is there a way to specify on what var s/// works  ?
 |  2) any better way to see if the pattern matched ?
 |  3) any smaller and faster way to do the same ?
 |   
 |   
 |  sub is_data {
 |    my $file = shift;
 |    $file=~s/(.*?)templatedata(*.?)data\\//sig;
 |    return $file;
 |  }
 |   
 |  sub is_binary {
 |      my $file = shift;
 |      $file=~s/^binaries//sig;
 |      return $file;
 |  }
 |   
 |  my @array1=();
 |  my @array2=();
 |  my @array3=();
 |  my @filelist=( "templatedata\\MyDir\\data\\0001.txt", 
 |        "otherdir\\test.yxy", "binaries\\movie.jpeg");
 |  my $dummy;
 |  foreach my $file (@filelist){
 |      $dummy=is_data($file);
 |      if($dummy eq $file){
 |          $dummy=is_binary($file);
 |          if($dummy eq $file){
 |              push @array3, $dummy;
 |          }
 |          else
 |          {
 |              push @array2, $dummy;
 |          }
 |      }
 |      else
 |      {
 |          push @array1, $dummy;
 |      }
 |  }
 |   
 `-------------------------------------------------

    You're having to do a lot more work because you're using s///
    instead of just // ( aka m// the match operator ). 

    Here is how I would do it: 

    my @filelist = ('some data here'); 
    my @array1 = (); 
    my @array2 = (); 
    my @array3 = (); 

    foreach my $file ( @filelist ) { 

        if( $file =~ /.*?\\templatedata\\.*?\\data/o ) { 
            push(@array1, $file); 
        }
        elsif( $file =~ /^binaries\\/o ) { 
            push(@array2, $file); 
        }
        else { 
            push(@array3, $file); 
        }
    }

    NOTE: The 'o' on the end of the regular expression matches instructs
    the perl interpretor to only compile the regex once, as it doesn't
    change during the foreach. 

 ---------------------------------
   Frank Wiles <[EMAIL PROTECTED]>
   http://frank.wiles.org
 ---------------------------------


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to