Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Comparing string to array elements

by Lotus1 (Priest)
on Jan 11, 2017 at 21:52 UTC ( #1179426=note: print w/replies, xml ) Need Help??

in reply to Comparing string to array elements

I noticed you used the s modifier in your match and that you seem to be trying to match across the whole file.

This example might not be the most efficient way but is a start.

use warnings; use strict; my @terms = qw( Maple Oak walnut pine juniper ); my $text; { #slurp the whole file local $/; $text = <DATA>; } if ( $text =~ /upperboundary(.+)lowerboundary/s ) { $text = $1; print $text, "\n\n"; } else { die "no match"; } foreach my $term (@terms) { if ( $text =~ /($term)/is ) { print "$1\n"; } } __DATA__ upperboundary The book Hiking the Red, A Complete Trail Guide To Kentucky's Red Rive +r Gorge made it a point to describe the habitat and the diverse species of tre +es that one is likely to encounter at the Red. According to the authors, the s +pecies of trees found in the Daniel Boone National Forest includes beech, sug +ar maples, white pines, hemlock, several types of oak, and hickory. These + trees provide habitat for an estimated 67 different species of reptiles and amphibians, 46 species of mammals, and 100 species of birds. lowerboundary

Edit: The text is from

Replies are listed 'Best First'.
Re^2: Comparing string to array elements
by R56 (Sexton) on Jan 13, 2017 at 15:07 UTC

    Hi Lotus,

    I used the s modifier because i needed the .+ to match new lines too. Is that the way to go?

      Yes, I used the s modifier in both of the regex's in my example. I changed my node a little to make clear I was saying my example might not be the most efficient. I wasn't referring to the use of the s modifier.

        Sorry for not responding earlier. I agree, it might be easier to do this than to compare 2 arrays. It's not as efficient as I've already tested it, but I'll work around it and see what i can do. Many thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1179426]
[LanX]: values %{ $DBI->fetchrow_has href('column_name' }
[LanX]: ehm ... fetchall_hashref
[Jenda]: my int @a = ^5_000_000 Coerces the argument to Numeric, and generates a range from 0 up to (but excluding) the argument.
[Jenda]: The apparent design principle of Perl6 operators was "Let's confuse 'em dudes." Whose braindead idea was this particular operator and the three hundred around it?

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2018-07-16 16:52 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (344 votes). Check out past polls.