Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Perl Match with variable

  • 18-04-2018 1:31pm
    #1
    Registered Users, Registered Users 2 Posts: 46


    Does anybody have any knowledge or experience on using perl match with a variable.

    The practical example I have is a variable $par_align which is set to J1.

    in this case the code:

    if ($var1 =~ m/$par_align/i){
    #do actions
    }

    will output any matches it finds, but what if I have a variable with more than one string in it. For example if $par_align = "J1, J2".

    Is there a way to include a wildcard to return data containing both J1 and J2?
    Tagged:


Comments

  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    Not sure if this is what your asking, but an array on the left hand side and a "g" modifier after the regex does what I think you want.
    [skrynesaver@busybox~]# perl -e 'my $text="one two three five";my @matches=$text=~/(one|three|five)/;print join("\n",@matches),"\n";' 
    one
    [skrynesaver@busybox ~]# perl -e 'my $text="one two three five";my @matches=$text=~/(one|three|five)/g;print join("\n",@matches),"\n";' 
    one
    three
    five
    


  • Registered Users, Registered Users 2 Posts: 46 TaytoMan69


    Sort of I suppose, values are held in an environment variable which limits a conversion to those values.

    I essence I can limit it now but im using perl match which I can only get to work as a single value i.e if $align = J1 it will find those matches, but if i add in a second variable J2 to the same value it does not return any match because it interprets those two elements or pieces of text as one logical block of text and will look for a match on J1 J2 instead of J1 and J2.

    your example will work fine but where my functionality differs is the fact that instead of using
    $text=~/(one|three|five)
    I am trying to supply it an environment variable containing values stored in one variable.

    so its more a case of figuring out whether this is possible with perl match. if it contains anything contained within the environment variable, print the matches.


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    #!/usr/bin/perl
    use strict;
    my @veg = qw(spuds cake carrots lettuce cheese tomatoes celery);
    my $favs = "cheese,cake";
    for my $var (@veg){
      if (check_for_break($favs,$var)){
        print "$var is in favourites\n";
      }
    }
    sub check_for_break{
      my $favs=shift;
      my $var=shift;
      for (split/,/,$favs){
        return 1 if $var=~/$_/;
      }
      return 0;
    }
    


Advertisement