Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Pattern Match in @array

by azatoth (Curate)
on Jun 11, 2001 at 18:51 UTC ( [id://87512]=perlquestion: print w/replies, xml ) Need Help??

azatoth has asked for the wisdom of the Perl Monks concerning the following question:

How do.

I need to check all the elements (files) in an array, and check them for a certain pattern. If it doesn't exist, I need to create that file.

So I'm wondering - a) Can I do this :

do_something unless (my $file (@files) =~ ^DB); # but i realise this won't loop over all the elements in the array, wh +ich is what i want :(
and b) What function can I use to "create" a new file?

Thanks Folks.

Azatoth a.k.a Captain Whiplash

Make Your Die Messages Full of Wisdom!
Get YOUR PerlMonks Stagename here!
Want to speak like a Londoner?

Replies are listed 'Best First'.
Re: Pattern Match in @array
by japhy (Canon) on Jun 11, 2001 at 18:59 UTC
    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
      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

Re: Pattern Match in @array
by premchai21 (Curate) on Jun 11, 2001 at 19:05 UTC
    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.
Re: Pattern Match in @array
by VSarkiss (Monsignor) on Jun 11, 2001 at 19:06 UTC
    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
Re: Pattern Match in @array
by marcink (Monk) on Jun 11, 2001 at 19:08 UTC
    You could use this:
    #!/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
Re: Pattern Match in @array
by Jouke (Curate) on Jun 11, 2001 at 19:08 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://87512]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found