Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

create search string from array elements

by ministry (Scribe)
on Jul 12, 2005 at 13:09 UTC ( #474266=perlquestion: print w/replies, xml ) Need Help??
ministry has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

As you can see my question stems from everyone's favorite perl function: pattern matching. My query is this, how can I take all elements of an array & turn them into search strings which would then be used for pattern matching against another array? In my example, the array @dates would contain anywhere from 2 to 50 elements in the following format; YYYYMMDD. While my second array, @logfiles would contain a list of log file names in the following format; logfile.system_name.YYYYMMDD (This list can contain anywhere from 2 to 200 items). How can I search through @logfiles for any elements that would contain any matches from @dates?

My first thought was to put a for loop inside a for loop, but after thinking about it, this seems like it would be a bit too process intensive for the heavily utilized routine this would be placed in. Any thoughts?

Cheers, Ev

Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
  • Comment on create search string from array elements

Replies are listed 'Best First'.
Re: create search string from array elements
by friedo (Prior) on Jul 12, 2005 at 13:19 UTC
    If you want to be clever you could make an alternating pattern from the list of dates:

    my $pat = join '|', @dates; my @files = grep /$pat/, @logfiles;

    But I have no idea if that would be faster or slower than just doing a nested loop. A nested loop at least gives you the option of skipping the remainder when you've found a match.

    my @files; foreach my $file( @logfiles ) { foreach my $date( @dates ) { if ( $file =~ /$date/ ) { push @files, $file; last; } } }
Re: create search string from array elements
by dave_the_m (Prior) on Jul 12, 2005 at 13:21 UTC
    my $pattern = join '|', map quotemeta, @dates; my $re = qr/^logfile\..*\.($pattern)$/; /$re/ && print "matched: $_\n" for @logfiles;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://474266]
Approved by marto
[Corion]: Last day at $work before 3 weeks of vacation (well, not that I'd be really travelling, but no-work-time ;) )
[robby_dobby]: marto: yeah, I'd eventually visit Glasgow some day - just to visit the home town of GHC :-)
[robby_dobby]: Corion: Oh, that's awesome! Your timing is perfect enough to see all hell break loose when you get back at work :P
[marto]: well, let me know in advance, I'll buy you a pint :)
[Corion]: robby_dobby: No, I'm returning in the second workweek of January. The main hectic parts are in the first days after the start of the new year
[robby_dobby]: marto: and, I'm not sure I'd be around here long enough for YAPC::EU
[marto]: Corion nice, what are you plans for Christmas?
[robby_dobby]: Corion: Nice, you just won't be around to deal with the mess - enjoy your vacation :-)
[robby_dobby]: marto: Sure, will do. Thanks!
[Corion]: marto: No great plans - I'll meet with my sister, my brother and my mother, but that's all :)

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-12-15 10:39 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.