[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

pattern matching problem

Hi ! I'm new in perl. Currently I'm developing a small online shop system. The script reads from a plain text database each line as one product. if you order a product, the line index is written into a hidden field and added to the other indexes in this field. if you want to remove an item I just replace the line index by ''. To prevent matching errors I'm adding '#'
before and after the index. now I've got a problem: If the user want to add the same product two times, my script doesn't catch this:
$art is the value of the Button (can be delete, too)
$artikel is the value of the hidden field where all the indexes are in
$artindex is the index to add, or to delete or not to add

     if ($art eq 'Bestellen')
                              {
                                     $temp  = "\#$artindex\#";
                                     if ($temp =~ $artikel) #<--- This doesn't work !
                                                                     {
                                                                            $artindex =~ s/\#//g;
                                                                            $temp = &GetEntry($artindex,1);
                                                                            &info ("Folgender Artikel wurde bereits von Ihnen in den Warenkorb gelegt\:\<br\>\"$temp\"");
                                                                     }
                                                                       else
                                                                     {
                                                                       $artikel = "$artikel\;\#$artindex\#";
                                                                          }
                              };

I just want to check, if #$artindex# is already in $artikel, why doesn't it work ???

regards
ItsMe
0
ItsMe
Asked:
ItsMe
  • 3
  • 3
1 Solution
 
prakashk021799Commented:
The comparison should be:

if ($artikel =~ m/$temp/) {
0
 
ItsMeAuthor Commented:
Hi !
I already tried this but it didn't work ! And: If one of the variables is empty, does it match or not ?
0
 
prakashk021799Commented:
> If one of the variables is empty, does it match or not ?

The general syntax for pattern matching is (without any modifiers):

$string =~ m/$pattern/;

If $string is empty, then result of the matching is false.
If $pattern is empty, then result of the matching is true.

Change the code so that you will search the $artikel string only if it is non-empty.

if ($artikel and $artikel =~ m/\#$artindex\#/) {
    # $artindex is already in the $artikel
    # do whatever you want to do
} else {
    # add $artindex to $artikel
    $artikel .= "\#$artindex\#';
}
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
prakashk021799Commented:
If you want to delimit $artindex by semi-colon (as you seem to be doing in your code), change the last line (the one above the closing brace) to:

    $artikel .= ";\#$artindex\#';
0
 
ozoCommented:
Does $artindex contain search metacharacters?
0
 
ItsMeAuthor Commented:
good job ! thank you very much ! i just had to flip the vars. as you did above. perhaps you know where i can find a manual about shop-structures or something else ... i only need a good strategy even I think mine isn't that bad, what do you think ???

regards
ItsMe
0
 
ItsMeAuthor Commented:
what are search metacarachters ?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now