Inline substitution
my $data = "spamspamspamspamusefuldataspamspamspam";
print("Data: " . (map { s/spam//g;$_; } ($data))[0] . "\n");
Note that this modifies $data.
Smart matching
Why do 2 vars with the following Devel::Peek::Dump output not match using !~~?
if ($expected !~~ $got) {
print("Smartmatch failed comparing data\n");
print("Expected:\n");
Devel::Peek::Dump $expected;
print("Got:\n");
Devel::Peek::Dump $got;
}
Smartmatch failed comparing data
Expected:
SV = PV(0x3e57478) at 0x3e80f80
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0x356d5f0 "0.00"\0
CUR = 4
LEN = 8
Got:
SV = PV(0x3e57498) at 0x3e9e4f0
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0xe0422e0 "0.00"\0
CUR = 4
LEN = 8
Answer: There is no such operator as !~~. Perl interprets it as $expected !~ (~ $got) which is entirely different from !($expected ~~ $got)
Excel reading issues
First the calling code from the testsuite:
sub ReadXLS {
my $ar_data;
print("Reading from xls...");
$benchmark = Benchmark::timeit(1, sub { $ar_data = $interface->Exc
+elBinary_ReadData("$Incoming_Dir/items.xls"); });
print(scalar @{$ar_data} . " records in [" . $benchmark->real . "
+seconds], [" . (scalar @{$ar_data} / $benchmark->real) . " records/s]
+\n");
return $ar_data;
}
And the librarydata that provides the ReadData-function:
$Interfaces::ExcelBinary::Headers = [];
# cell_handler (Workbook, Sheet_index, Row, Col, Cell)
# Called by Spreadsheet::ParseExcel for every cell encountered.
sub cell_handler {
my ($workbook, $sheet_index, $row, $col, $cell) = @_;
if ($row == 0) { push(@{$Interfaces::ExcelBinary::Headers}, $cell-
+>value); }
if ($row > 0) {
Data::Dump::dd($Interfaces::ExcelBinary::Headers);
$workbook->ParseAbort(1);
exit;
}
}
# ReadData (Filename, [WorkSheetID]) returns $ar_data
# Reads data from the given file (which should be a BIFF-formatted .xl
+s-file) and the given worksheet (by name or number (0-based)).
# If the supplied worksheetID is a number, a negative number -n will r
+efer to the n-to-last worksheet.
sub ReadData {
my ($self, $FileName, $WorkSheetID) = @_;
my $ExcelParser = Spreadsheet::ParseExcel->new(
CellHandler => \&cell_handler,
NotSetCell => 1,
);
print("Parsing\n");
my $WorkBook = $ExcelParser->parse($FileName);
print("Done parsing\n");
exit;
Remove common prefix
Idea based on the palindrome-detection algorithm.
#!/usr/bin/perl
use warnings;
use strict;
my $text1 = 'aapnootmies';
my $text2 = 'aapnogeenaap';
my $text3 = $text1 . reverse($text2);
$text3 =~ s/(.*)(.*)(??{reverse $1})/$2/;
print("Without common start [$text3]\n");
moritz: oh, that's a nice combination of "clever" and "stupid" :-)
He then fixed it up to be like this:
#!/usr/bin/perl
use warnings;
use strict;
my $text1 = 'aapnootmies';
my $text2 = 'aapnogeenaap';
my $text3 = $text1 . reverse($text2);
$text3 =~ m/(.*)(.*)(??{quotemeta reverse $1})/s;
my $common = $1;
$text1 =~ s/^\Q$common//;
$text2 =~ s/^\Q$common//;
print "[$text1]\n[$text2]\n";
|