简体   繁体   中英

Perl Regex finding value too small and appending

I am working on an assignment which requires me to read in a bunch of names and phonebook and essentially create a search that works much like the dialer on your phone. If a number only consists of xxx-xxxx then an area code should be appended. I am successfully able to pick out strings that consist of xxx-xxxx but when I try to print that string I just get an empty line. When I call the previous index, it successful prints out the name.

foreach my $i (0 .. $#phoneBook) {
    my @splitIndex = split(':', $phoneBook[$i]); #index 0 and 1 of splitIndex
    #If the length of the number is too short append the 701 before
    if($splitIndex[1] =~ s/^(\d{3})-(\d{4})//) {
      print "$splitIndex[1]\n";
    }
    foreach my $j (0 .. $#splitIndex) {
      #print "$splitIndex[$j]";
    }
    if(($splitIndex[0] =~ /$searchValue/i) || ($splitIndex[1] =~ /$searchValue/i)) {
      #print "Found\n";
    } else {
      #print "Not Found $searchValue\n";
    }
  }

In my file, the text John:888-8888 is saved in my phoneBook array. But when I call splitIndex[1] (the phone number) I get an empty line.

The issue was I was using the substitute rather than the match tag in the line.

$splitIndex[1] =~ s/^(\d{3})-(\d{4})//

To

$splitIndex[1] =~ m/^(\d{3})-(\d{4})/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM