Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Dereference inside a regex

by tobias_hofer (Friar)
on Jun 07, 2013 at 14:21 UTC ( #1037680=perlquestion: print w/replies, xml ) Need Help??
tobias_hofer has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

As it is possible to use variables in a regex like ~m/$myvar/;. Then why it is not possible to do something like that:
my $state_smybol = { 1 => 'Image component sizes', 2 => ' Code \(inc\. data\) RO Data RW Data ZI Dat +a Debug Object Name', }; ... if( $MapFile->[$iterator] =~m/$state_symbol->{1}/){..
The regex has uninitialized values then.. ?
Actually i do not want to create an additional temporary variable for storing the value from the hash and then doing the regex..

Any help is highly welcome!

Best regards!

Replies are listed 'Best First'.
Re: Dereference inside a regex
by hippo (Abbot) on Jun 07, 2013 at 15:51 UTC

    The problem is that your hashref is declared as $state_smybol but you refer to it as $state_symbol which is clearly different. use strict would have caught that.

      This is it! Thanks a lot! Thank you for saving me hours of searching!!!
      As I am getting blind I will go for weekend now ;-)

      Cheers! Tobias
        -- for not using strict! =(

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Dereference inside a regex
by hdb (Monsignor) on Jun 07, 2013 at 14:29 UTC

    The following works for me:

    use strict; use warnings; my $hashref = { 1 => 'c' }; print "success" if "success" =~ /$hashref->{1}/;
Re: Dereference inside a regex
by AnomalousMonk (Chancellor) on Jun 07, 2013 at 14:55 UTC
    The regex has uninitialized values then.. ?

    What uninitialized values? Can you give a short, standalone program that produces these uninitialized values?

      ya,.. its also strange to me
      here the code in its current state:
      sub AnalyseMapFile { my $self = shift; my $MapFile = $self->{MAPFILEINFO}{MAPFILECONTENT}; my $PlaceToPutInformation = $self->{MEMORYMAP}; $self->{MAPFILEINFO}{ProfileInformation} = $ProfileInformation; my $memory_section = ''; my $state = 0; my $memory_size = 0; my $state_smybol = { 1 => 'Image component sizes', 2 => ' Code \(inc\. data\) RO Data RW Data ZI Dat +a Debug Object Name', }; #Each config profile defines its own way how to parse the map-file +. #Process map-file for ( my $iterator = $ProfileInformation->{'StartLine'} ; $iterator < @$MapFile ; $iterator++ ) { chomp($MapFile->[$iterator]); next if (! $MapFile->[$iterator]); if (( $state == 0 ) ###THIS IS LINE 104 && ( $MapFile->[$iterator] =~m/$MTA::Profile_ARM::state_symbo +l->{1}/)) { $state = 1; next; } elsif (( $state == 1 ) && ( $MapFile->[$iterator] =~m/$MTA::Pr +ofile_ARM::state_symbol->{2}/)) { $state = 2; next; } elsif ( $state == 2 ) { #Get module information my($Code, $CodeData, $ROData, $RWData, $ZIData, $Debug, $O +bjName) = split(/\s/,$MapFile->[$iterator]);
      and I get this message from perl-diagnostics, moreover i am using eclipse and epic-plugin and strawberryperl 5.10.0.
      The warning looks like this:
      Use of uninitialized value in regexp compilation at D:/_Debug/Project/ line 104 (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell y +ou the name of the variable (if any) that was undefined. In some cases it + cannot do this, so it also tells you what operation you used the undefine +d value in. Note, however, that perl optimizes your program and the opera +tion displayed in the warning may not necessarily appear literally in y +our program. For example, "that $foo" is usually optimized into "that + " . $foo, and the warning will refer to the concatenation (.) operat +or, even though there is no . in your program.

      Ok, i am just guessing that this is because of the dereferencing, but the scalar
      $state is initialized with '0'. So I am running out of plans...

        Before I saw your reply below indicating your problem is fixed, I was going to suggest you take a look at the package global variable  $MTA::Profile_ARM::state_symbol to see how it was defined; you have apparently done this and found the problem. And, yes, using strictures and warnings is always a good idea, especially if you are a novice Perler.

        The misleading line number reported for the "uninitialized value in regexp compilation" warning is due to a quirk (dare one say bug?) present until recently (I'm too lazy to look up the version in which this was fixed) in the compiler such that the line number of the first line of a statement block (e.g., an if-statement) is the line number reported for any warning (or error?) encountered within the statement block. This can be disorienting, but the warning message did clearly refer to a "regex compilation" problem, and there are only two regexes associated with the if-statement in question, and these regexes consist entirely in the interpolation of some value from a single global hash, so the obvious place to look should have been the  $MTA::Profile_ARM::state_symbol global.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037680]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-04-25 03:21 GMT
Find Nodes?
    Voting Booth?