Two regular expressions are not compared with If-Statement

I want to compare two regular expressions in perl, but this fails:

Using an uninitialized value in numeric eq & lt == == in [... data file and string]. Argument [another regular expression!] Not numeric in numeric eq <==> [...]

I want to store the regex in an array when the value does not exist yet. Therefore, I compare the current regex (tmpvar) with all elements of the array.

Is it possible at all or do I need to use a bypass?

$pass = new String::Random; my $array_counter=0; my @regex_array = (); push(@regex_array, $pass->randregex($regex_from_file)); #print "@regex_array"; $array_counter++; while ($array_counter != $anzahl_regex) { print $array_counter; my $tmpvar = $pass->randregex($regex_from_file); my $tmpcheck = 0; my $tmparraylength = scalar (@regex_array); for ($i=0; $i<= $tmparraylength ;$i++) { if ($tmpvar == $regex_array[$i]) # ERROR { $tmpcheck =1; } } if ($tmpcheck == 0) # es wurde kein duplikat gefunden { push(@regex_array,$tmpvar); $arraycounter++; } $arraycounter++; } 
+4
source share
3 answers

== used to compare numbers. Use eq to compare strings:

 if ($tmpvar eq $regex_array[$i]) 

In addition, you go to the end of the regex_array loop in the for loop:

 for ($i=0; $i < $tmparraylength ;$i++) ^ this must not be <= 

Finally, you work too much. Use a hash, it automatically "removes duplicate keys."

 my %temp_hash; while (scalar(keys %temp_hash) < number_of_things_you_want) { $temp_hash{$pass->randregex($regex_from_file)}++; } @regex_array = keys %temp_hash; 
+6
source

Do not use numerical comparison == with eq string comparison.

See here documentation at perldoc.perl.org

+2
source

If all you want to do is nullify the array, this might be more appropriate:

 { my %h; map { $h{$_} = 1 } @all_regex; @all_regex = keys %h; } 

Note that it does not preserve the order of the original array.

0
source

All Articles