in reply to Re: Regex and array
in thread Regex and array

Hi Corion and everyone

thanks in advance for your answer. The problem is not that it can't read the file. The lines i posted above where just there for shorten the problem. I usually put the "use strict" pragma and the die() instruction in my scripts to prevent what you correctly pointed out.

This time, indeed, the problem is that if I try to match the array's elements without cycling them with a foreach, I can't get a match.

I'll try to post a maybe better example

#!/usr/bin/perl my @array = qw( ciccio paperino palla gigio pluto ); my $test = pluto; if ( $test =~ $array[0..$#array] ) { print "It matched!\n"; }
didn't work for me. Is the syntax wrong? Am I missing something? Thanks again for your help.

Replies are listed 'Best First'.
Re^3: Regex and array
by toolic (Bishop) on May 25, 2010 at 12:47 UTC
    Am I missing something?
    Yes, you are ignoring important warnings because you did not use strict and warnings.

    One way to search an array for a specific value is to use grep. I think you are looking for something like this:

    use strict; use warnings; my @array = qw( ciccio paperino palla gigio pluto ); my $test = 'pluto'; if (grep { $test eq $_ } @array ) { print "It matched!\n"; }

      Thanks toolic.

      As I've been explaining to Corion, I use to use the "strict" pragma in my scripts. The code I posted before, just was a shortened example to focus more on the problem i was experiencing.

      Thanks to your suggestion, with grep() applied to the array, I've been able to make the code work! Thank you very much for your help!

Re^3: Regex and array
by Corion (Patriarch) on May 25, 2010 at 12:41 UTC

    You are making up Perl syntax and expect it to work. I don't think that array slices and the =~ operator are documented to do anything sensible.

    Again, you are not using strict. You should really get into the habit of doing that.

    If you are using Perl 5.10 or newer, you might be able to put the "smart match" operator (~~) to work. See perlop on it. Otherwise, you could use grep to search through the array.

      Thanks corion.

      As I already wrote before, these that I posted were just examples to shorten the code to post on here. In real life, I really do use "strict" and "die".

      Unfortunately I work with a perl 5.8 interpreter, so I can't use the smart match operator (the functionality of which was the basis for my argumentation).

      I'll try the "grep" suggestion :)

      Thanks

Re^3: Regex and array
by vtheron (Novice) on May 25, 2010 at 21:38 UTC
    I've used the following technique successfully for this kind of situation. It compiles the regex only once, so it can be reused efficiently in a loop:
    my @array = qw( ciccio paperino palla gigio pluto ); my $TestReStr = join("|", map { "(${_})" } @array); my $TestRe = qr/$TestReStr/; my $test = 'pluto'; print "It matched!\n" if $test =~ $TestRe ;

      Nice idea, even to speed up the code!

      Thanks!