-2

I have stored a piece of string "Strawberry Hill" in a variable. However I am trying to use a regular expression to see if the string matches a particular format. If the string matches I would like to store it in a variable. However nothing gets stored in the variable. I tested the regular expression in the same script outside of the if statement and it worked.

  my $address="STRAWBERRY HILL";

    if($address=~ m/(\w+\s+\w+)/)
    {
        my $scheme_name=$1;
    }
    print " Scheme Name: $scheme_name";
A.Ban
  • 43
  • 1
  • 1
  • 6
  • Scope issue: [see here](https://tio.run/##K0gtyjH9/z@3UkElMSWlKLW42FYpOCTIMdzJNSgoUsHD08dHyZoLJF2cnJGamxqfl5ibas3FlZmmAddQp5CrrxFTrh1TrA0kNfU1uaq5OJHVK9gqqBhac9VyFRRl5pUoKCkEg@UU/IByVigmK1n//w8A) -> moved this to [answer](https://stackoverflow.com/a/48120612/3600709). – ctwheels Jan 05 '18 at 20:08
  • 4
    Only one thing to say `use strict; use warnings;` –  Jan 05 '18 at 20:39

2 Answers2

2

First of all, always use use strict; use warnings qw( all );. Your error would have been found at compile-time.

my creates a new variable which is scoped to (only visible in) the inner-most block in which it resides.

There are many possible solutions.

  • Move the print.

    if ( my ($scheme_name) = $address =~ /(\w+\s+\w+)/ ) {
        print "Scheme Name: $scheme_name\n";
    } else {
        print "Scheme Name: default\n";
    }
    
  • Create the variable before the if.

    my $scheme_name = "default";
    if ($address =~ /(\w+\s+\w+)/) {
        $scheme_name = $1;
    }
    
    print "Scheme Name: $scheme_name\n";
    
  • Switch from using if to using the conditional operator.

    my $scheme_name = $address =~ /(\w+\s+\w+)/ ? $1 : "default";
    print "Scheme Name: $scheme_name\n";
    
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

Brief

$scheme_name needs to be defined outside the if block. If the if test were false then $scheme_name wouldn't be declared.

As per Perl's documentation of my:

A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval. If more than one variable is listed, the list must be placed in parentheses.


Code

See code in use here

my $address="STRAWBERRY HILL";
my $scheme_name="";

if($address=~ m/(\w+\s+\w+)/)
{
    $scheme_name = $1;
}
print " Scheme Name: $scheme_name";
ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • This is horrible. You can't distinguish between declaration and definition; there is no *if statement* and so it cannot *return* anything. The problem isn't that *"`$scheme_name` never actually gets set"* but that it doesn't exist. Please stop "helping" with your Perl insights. – Borodin Jan 05 '18 at 22:29
  • And please use *markdown* and avoid HTML – Borodin Jan 05 '18 at 22:33
  • 2
    @Borodin yes I agree that I should have been more careful with my terminology, that's completely my fault and I do appreciate you pointing out those mistakes and correcting them for me. Was it really necessary to remove all the additional information from my question though? I've added back the coding sample, link to tio (to see it running) and the link to Perl's documentation. I believe these pieces are key for any programmer to succeed: Know where to find the information and give an example so that they can see exactly how to implement the solution. Thank you for helping me improve my answer – ctwheels Jan 06 '18 at 21:23