tags:

views:

56

answers:

1

I am parsing an XML file, and after extracting some information, I want to see if a particular field has a '/' (slash) in it. So I do the following code:

  if (defined($orgUIDLookup{$orgUidMid}))
  {
    my $country = $orgUIDLookup{$orgUidMid};
    print "country = $country ";
    if ($country !~ ?/?)
    {
      print "OK\n";
      $airportRef{country} = $country;
    }
    else
    {
      print "no good\n";
      $needHelp = 1;
    }
  }

But I seem to be getting inconsistent results for the nodes with a slash in them:

grep '^country = .*/' foo
country = CN/MA/RE no good
country = CN/MA/RE OK
country = CN/MA/RE OK
country = CN/MA/RE OK
country = RB/MJ OK
country = RB/MJ OK
country = RB/MJ OK
country = CN/MA/RE OK
country = CN/MA/RE OK
country = RB/MJ OK
country = RB/MJ OK
country = CN/MA/RE OK
country = CN/MA/RE OK
country = RB/MJ OK
country = RB/MJ OK
country = KR/TV OK
country = KR/TV OK
country = KR/TV OK
country = WS/AQ OK
country = AA/NT OK
country = AA/NT OK
country = AA/NT OK
country = AA/NT OK
country = AA/NT OK
country = AA/NT OK

Why would this test would go through the else branch ok the first time, but go through the if branch every other time?

+4  A: 

This is just like the /pattern/ search, except that it matches only once between calls to the reset() operator. This is a useful optimization when you want to see only the first occurrence of something in each file of a set of files, for instance. Only ?? patterns local to the current package are reset.

just use /\// or m!/! or something of the sort. ?? has some usually-bad-idea magic associated with it.

sreservoir
I never realized ?? was special - I use it all the time in sed where it just means I don't have to escape slashes.
Paul Tomblin
`m#foo#` is really nice and visually distinctive, although it breaks some syntax highlighters.
hobbs
I used to use `m#foo#` until vim's syntax highlighting convinced me that `m{}` and `s{}{}` was nicer.
Ether
that's why you rewrite the syntax files!
sreservoir