in reply to Regex and array

Are you sure that the match part won't work? Have you checked that you can open your file and read the lines you expect?

Adding the line

use strict;

to the top of your script will make Perl tell you what it sees. Then you will find that what you see is not what Perl sees.

You should always guard your calls to open with an or die like this:

open my $file, $filename or die "Couldn't open '$filename': $!";

That way, you prevent your program from running when it can't read a file and you even get an informative error message.

Replies are listed 'Best First'.
Re^2: Regex and array
by Deus Ex (Scribe) on May 25, 2010 at 12:34 UTC

    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.
      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!

      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

      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!