Just loop over the array, one element at a time:
my $found;
for (@list) {
last if $found = /pattern/;
}
if (!$found) {
open FILE, "> $filename";
close FILE;
}
japhy --
Perl and Regex Hacker | [reply] [d/l] |
| [reply] [d/l] |
I may be going against the advice of mirod : "correct a mistake by a high-ranking monk: yeah, like anybody's going to believe you!" but oh well : )
Actually, I believe (I may be wrong, it wasn't too clear), that the poster has an array of files, and he wants to check each one against a pattern, and if it doesn't match, then he will make the file. If the first file in the area of your example is good, the whole thing stops. Merely change that last to a next, and open the file right after you write next:
for (@files) {
next if /pattern/;
#file didn't match pattern, so create it
open(FH,">$_");
#do something with FH
}
The 15 year old, freshman programmer,
Stephen Rawls | [reply] [d/l] |
This solves both your problems (if I interpret you correctly):
foreach my $file (grep { !-e } @files) # The grep returns all the elem
+ents of the array
# for which the sub ( { !-e } )
+ returns true, which it
# will only if $_ (the array el
+ement being tested) does not
# exist. If you want it to exi
+st and be a
# regular file (as opposed to a
+ directory, FIFO, etc.)
# use -f.
{
local *FH; # To make sure we don't clobber
+ FH.
open FH, ">>$file"; # In case the file got created
# in the meantime, we use the a
+ppend operator to avoid
# clobbering data.
close FH; # And then we close it.
}
See also: foreach grep local open -X. | [reply] [d/l] |
Sounds like grep might be the ticket here.
do_something($_) foreach grep(/DB/, @files);which will call do_something with every element of the @files array that has "DB" somewhere in it.
As to part (b), there's no Perl built-in equivalent to the creat system call (not that I'm aware of). If you want to create a zero-length file with a particular name, you can just write to it:
open(I, ">$filename");
close(I);
Alternatively, if you're on something Unix-y, you can use the shell to "touch" the file:
system("touch $filename");
HTH | [reply] [d/l] [select] |
#!/usr/bin/perl -w
use strict;
my @arr = qw( file1 file2 file3 filename );
print "ha!\n" unless grep /^filename$/, @arr;
See also the '-e' operator -- you don't have to list a directory to check if a file exists.
-mk | [reply] [d/l] |
The syntax you'd want would be something like:
@array = ('bla', 'alb', 'lab');
foreach (@array) {print unless /la/};
And to open a file, simply open it in write mode (by adding '>')
Jouke Visser, Perl 'Adept'
Using Perl to help the disabled: pVoice and pStory
| [reply] [d/l] |