微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何获取字符串开头对应字符的个数

如何解决如何获取字符串开头对应字符的个数

我有两个字符串,例如$must$is。它们在开始时应该是相同的。但是如果有错误,我想知道在哪里。

示例:

my $must = "abc;def;ghi";
my $is   = "abc;deX;ghi";

位置 6 上的“X”不相等,所以这是我需要的结果。

所以我需要类似的东西

my $count = count_equal_chars($is,$must);

结果是“6”,因为字符 0 到 5 是相等的。 (如果是“5”也没关系,因为写 ++ 没问题。)

到目前为止我的代码

编辑添加一个解决方法

#!/usr/bin/perl

use strict;
use warnings;
use utf8;

my $head_spec = "company;customer;article;price"; # specified headline
my $count = 0;                                    # row counter

while (<DATA>) {
    s/[\r\n]//;      # Data comes originally from Excel ...
    if (!$count) {
        # Headline:

        ## -> error message without position:
        ##print "error in headline\n" unless ($head_spec eq $_);

        ## -> writeout error message with position:
        next if ($head_spec eq $_);
        # Initialize char arrays and counter
        my @spec = split //,$head_spec; # Specified headline as character-array
        my @is   = split //,$_;         # Readed    headline as character-array
        my $err_pos = 0;                 # counter - current position
        # Find out the position:
        for (@spec) {
             $err_pos++,next if $is[$err_pos] eq $_;
             last;
        }
        # Writeout error message
        print "error in headline at position: $err_pos\n";
    }
    else {
        # Values
        print "process line $count,values: $_\n";
    }
}
continue { $count++; }

__DATA__
company;custXomer;article;price
Ser;0815;4711;3.99
Ser;0816;4712;4.85

背景:
背景是,有一个带有很长标题(> 1000 个字符)的 .csv 文件。此标头指定。 如果其中有错误,则该文件错误,必须由用户进行编辑。 所以告诉他错误在哪里很有用,这样他就不需要比较整行了。

解决方法

使用xor^,运算符可以找到错误位置。在连续字母匹配的情况下,异或运算给出 \0

$-{0]last match start variable(对于上一行中的正则表达式,(($must ^ $is) =~ /[^\0]/)。

你可以这样找到位置:

#!/usr/bin/perl
use strict;
use warnings;

my $must = "company;customer;article;price";
my $is   = "company;custXomer;article;price";


($must ^ $is) =~ /[^\0]/; # find first non-matching character

print "Error position is ",$-[0];  # position of first non-matching char

打印:12

,

我们可以逐个字符地进行比较,并在边缘情况下考虑字符串的长度:

use strict;
use warnings;
use List::Util qw<min max>;

my $must = "company;customer;article;price";
my $is   = "company;custXomer;article;price";

# to-be reported position
my $pos = 0;

# get minimum and maximum of the lengths
my @lengths = map length,($must,$is);
my $min_length = min @lengths;
my $max_length = max @lengths;

# increment till an inequality occurs or a string is consumed fully
++$pos until substr($must,$pos,1) ne substr($is,1) || $pos == $min_length;

# report the result
print $pos == $min_length ? ($pos < $max_length ? "missing cols" : "no diff") : $pos;

如果最后位置等于最小长度,则有 2 个选项:它们完全相等或一个更长,因此我们检查最大长度。否则,按原样报告位置。

,

以下代码

  • 将行拆分为两个字符数组 @must@is
  • 比较数组的长度并在它们不同时发出警告
  • 然后比较数组直到第一次不匹配
  • 如果 $pos 与 $must 字符串的最后一个索引不匹配,则打印错误
use strict;
use warnings;
use feature 'say';

my $must = "abc;def;ghi";
my $is   = "abc;deX;ghi";

my @must = split('',$must);
my @is   = split('',$is);
my $pos;

warn "Warning: length is differ"
    unless $#must == $#is;

for ( 0..$#must ) {
    $pos = $_;
    last unless $must[$pos] eq $is[$pos];
}

say "Error: The strings differ at position $pos"
    unless $pos == $#must;

输出

Error: The strings differ at position 6

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。