Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Neighbour's scratchpad

by Neighbour (Friar)
on Nov 11, 2009 at 12:57 UTC ( #806481=scratchpad: print w/replies, xml ) Need Help??

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";
Log In?

What's my password?
Create A New User
[gnosti]: Hi, Looking for a module that can add HH:MM:SS durations like 1:40, 1:00:24.13 and 12.3? I see on CPAN more modules with absolute dates. TIA
[kcott]: s/imput/input/
[Corion]: DateTime::Duration ? Personally,I convert stuff to seconds using Time::Piece or Time::Local and then add the seconds and then use POSIX::strftime to print them as hours again
[marto]: good morning all, TCIF
[gnosti]: I'm reading from ffmpeg lengths of video clips in H:M:S and needing to add them and format them back to H:M:S for feeding back to ffmpeg
[marioroy]: kcott Thanks
[Corion]: gnosti: I would cheat and use my approach instead of dealing with durations as objects, or a module

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-08-18 08:57 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (297 votes). Check out past polls.