I have two strings e.g. $must and $is. They should be identical at the start. But if there is an error, I want to know where.
Example:
my $must = "abc;def;ghi";
my $is = "abc;deX;ghi";
The "X" on position 6 is not equal, so this is my result I need.
So I need something like
my $count = count_equal_chars($is, $must);
where the result is "6" becaus character 0 to five are equal.
(Don't matter if it is "5" because writing ++ is no problem.)
My code so far:
EDIT: Added a workaround.
#!/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
Background:
The background is, that there is a .csv-file with a very long header (>1000 characters). This header specified.
If there is an error in it, the file has an error and must be edited by the user.
So it is useful to tell him, where the error is, so he doesn't need to compare the whole line.