SlideShare a Scribd company logo
How to
clean an array
Perlish
more-than-one-way
We hired
 Delphi/SQL
programmer
as a Perl developer
Good programmer
writes good code
in every language
But lots of fun
sub clear{
   my $template = shift @args;
   @{$self->{tokens}} = ();
   foreach (map {$_->{name}} @{$template->{si}}){
       while (my $r = shift @{$self->{$_}->{res}}){}
   }
}
Wat de fuck is dat?
sub clear{
   my $template = shift @args;
   @{$self->{tokens}} = ();
   foreach (map {$_->{name}} @{$template->{si}}){
       while (my $r = shift @{$self->{$_}->{res}}){}
   }
}



 A subroutine to make an object empty
sub clear{
   my $template = shift @args;
   @{$self->{tokens}} = ();
   foreach (map {$_->{name}} @{$template->{si}}){
       while (my $r = shift @{$self->{$_}->{res}}){}
   }
}



                    Note this
Posted that piece to
Moscow.pm mailing list
And we bacame crazy
1

while (my $r = shift @a) {}
2

splice @a;
3

delete(@a[0..$#a]);
4

grep {shift @a} @a;
5

@a = ();
5

@a = ();



Boring!
6

undef @a;
ccn@ccn-laptop ~
$ perl -le 'my @a=(1,2,3); while(my $r =
shift @a){}; print @a'

ccn@ccn-laptop ~
$ perl -le 'my @a=(0,1,2,3); while(my $r
= shift @a){}; print @a'
123
7

while(defined(my $r = shift @a))
              {}
ccn@ccn-laptop ~
$ perl -le 'my @a=(undef,1,2,3);while
(defined(my $r=shift @a)){};print @a'
123
Doesn’t work
Let’s make it
   useful
$str = "abcc{0}def";
@a = split //, $str;
$c++ while(shift @a);
say $c - 1;


Calculate zero-ending string length
8

while(chomp $str) {};
9

while(@a) {shift @a}
10

shift @a while @a;




    Much better!
11

shift @a while @a and die;
12

shift @a while scalar @a;
13

 $#a = -1




Who remembers?!
Bonus

my @a = qw(oh eh);
while (push(@a, shift @a)) {};



          Endless sex loop
use Benchmark qw(:all :hireswallclock);
my $size = 1000;                                            Be
cmpthese timethese -3, {                                         nc
   '@a = ...'          => sub { my @a = (('test')x$size);           h    m
       return; },                                                         ar
   'undef @a'          => sub { my @a = (('test')x$size); undef @a;          ke
       return; },                                                               d
   '@a=()'             => sub { my @a = (('test')x$size); @a = ();
       return; },
   'splice @a'         => sub { my @a = (('test')x$size); splice @a;
       return; },
   '$#a = -1'          => sub { my @a = (('test')x$size); $#a = -1;
       return; },
   'shift @a while @a' => sub { my @a = (('test')x$size); shift @a
while @a;  return; },
   'delete @a[0..#$a]' => sub { my @a = (('test')x$size);
delete(@a[0..$#a]); return; },
};

при $size=1000;
shift @a while @a 2642/s                --              -22%      -44%
   -44%  -45%     -45%     -45%
delete @a[0..#$a] 3376/s               28%                --      -28%
   -29%  -29%     -30%     -30%
splice @a         4722/s               79%               40%        --
    -0%   -1%      -2%      -2%
TIMTOWTDI or die;




         Andrew Shitov

talks.shitov.ru | andy@shitov.ru

More Related Content

What's hot (20)

DOCX
Cpp programs
harman kaur
 
ODP
Cli deep dive
Edward Capriolo
 
PDF
Designing Immutability Data Flows in Ember
Jorge Lainfiesta
 
PDF
Implementation of c string functions
mohamed sikander
 
PDF
The Ring programming language version 1.3 book - Part 14 of 88
Mahmoud Samir Fayed
 
PPT
C questions
mohamed sikander
 
PPTX
Python lec2
Swarup Ghosh
 
PPT
FMDB - SLC-Cocoaheads
Dave Stevenson
 
KEY
Tres Gemas De Ruby
Leonardo Soto
 
PDF
3分くらいで分かるassert()
Ippei Ogiwara
 
PDF
Camping
Gregor Schmidt
 
PDF
Views&urls
Camilla Ke
 
PDF
[Php] navigations
Thai Pham
 
PDF
Doubly Linked List
Er. Ganesh Ram Suwal
 
PDF
Linear queue
Er. Ganesh Ram Suwal
 
PDF
Mission vim possible-full
Sam Gottfried
 
Cpp programs
harman kaur
 
Cli deep dive
Edward Capriolo
 
Designing Immutability Data Flows in Ember
Jorge Lainfiesta
 
Implementation of c string functions
mohamed sikander
 
The Ring programming language version 1.3 book - Part 14 of 88
Mahmoud Samir Fayed
 
C questions
mohamed sikander
 
Python lec2
Swarup Ghosh
 
FMDB - SLC-Cocoaheads
Dave Stevenson
 
Tres Gemas De Ruby
Leonardo Soto
 
3分くらいで分かるassert()
Ippei Ogiwara
 
Views&urls
Camilla Ke
 
[Php] navigations
Thai Pham
 
Doubly Linked List
Er. Ganesh Ram Suwal
 
Linear queue
Er. Ganesh Ram Suwal
 
Mission vim possible-full
Sam Gottfried
 

Similar to How to clean an array (20)

KEY
YAPC::Tiny Introduction
Kang-min Liu
 
PDF
Dades i operadors
Alex Muntada Duran
 
PDF
Learning Perl 6 (NPW 2007)
brian d foy
 
PDF
Scripting3
Nao Dara
 
PDF
Learning Perl 6
brian d foy
 
PPT
Introduction to Perl
Sway Wang
 
PPT
Dealing with Legacy Perl Code - Peter Scott
O'Reilly Media
 
PDF
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
PPT
Crash Course in Perl – Perl tutorial for C programmers
Gil Megidish
 
PDF
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
DOCX
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
PPT
CGI With Object Oriented Perl
Bunty Ray
 
PDF
Memory unmanglement
Workhorse Computing
 
PDF
tutorial7
tutorialsruby
 
PDF
tutorial7
tutorialsruby
 
PPTX
Array,lists and hashes in perl
sana mateen
 
PDF
Lecture4
tutorialsruby
 
PPTX
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
 
PDF
Lecture4
tutorialsruby
 
YAPC::Tiny Introduction
Kang-min Liu
 
Dades i operadors
Alex Muntada Duran
 
Learning Perl 6 (NPW 2007)
brian d foy
 
Scripting3
Nao Dara
 
Learning Perl 6
brian d foy
 
Introduction to Perl
Sway Wang
 
Dealing with Legacy Perl Code - Peter Scott
O'Reilly Media
 
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
Crash Course in Perl – Perl tutorial for C programmers
Gil Megidish
 
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
CGI With Object Oriented Perl
Bunty Ray
 
Memory unmanglement
Workhorse Computing
 
tutorial7
tutorialsruby
 
tutorial7
tutorialsruby
 
Array,lists and hashes in perl
sana mateen
 
Lecture4
tutorialsruby
 
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
 
Lecture4
tutorialsruby
 
Ad

More from Andrew Shitov (20)

PDF
Perl jobs market in 2024, how good is it?
Andrew Shitov
 
PDF
Perl6 one-liners
Andrew Shitov
 
PDF
Creating a compiler in Perl 6
Andrew Shitov
 
PPTX
Fun with Raspberry PI (and Perl)
Andrew Shitov
 
PDF
Perl6 in-production
Andrew Shitov
 
PDF
Параллельные вычисления в Perl 6
Andrew Shitov
 
PDF
AllPerlBooks.com
Andrew Shitov
 
PDF
Perl 6 for Concurrency and Parallel Computing
Andrew Shitov
 
PDF
The Joy of Smartmatch
Andrew Shitov
 
PDF
YAPC::Europe 2013
Andrew Shitov
 
PDF
Perl 7, the story of
Andrew Shitov
 
PDF
Язык программирования Go для Perl-программистов
Andrew Shitov
 
PDF
Как очистить массив
Andrew Shitov
 
PDF
What's new in Perl 5.14
Andrew Shitov
 
PDF
Что нового в Perl 5.14
Andrew Shitov
 
PDF
Perl6 grammars
Andrew Shitov
 
PDF
Text in search queries with examples in Perl 6
Andrew Shitov
 
PDF
Perl 6 by example
Andrew Shitov
 
PDF
Perl 5.10 и 5.12
Andrew Shitov
 
PDF
Say Perl на весь мир
Andrew Shitov
 
Perl jobs market in 2024, how good is it?
Andrew Shitov
 
Perl6 one-liners
Andrew Shitov
 
Creating a compiler in Perl 6
Andrew Shitov
 
Fun with Raspberry PI (and Perl)
Andrew Shitov
 
Perl6 in-production
Andrew Shitov
 
Параллельные вычисления в Perl 6
Andrew Shitov
 
AllPerlBooks.com
Andrew Shitov
 
Perl 6 for Concurrency and Parallel Computing
Andrew Shitov
 
The Joy of Smartmatch
Andrew Shitov
 
YAPC::Europe 2013
Andrew Shitov
 
Perl 7, the story of
Andrew Shitov
 
Язык программирования Go для Perl-программистов
Andrew Shitov
 
Как очистить массив
Andrew Shitov
 
What's new in Perl 5.14
Andrew Shitov
 
Что нового в Perl 5.14
Andrew Shitov
 
Perl6 grammars
Andrew Shitov
 
Text in search queries with examples in Perl 6
Andrew Shitov
 
Perl 6 by example
Andrew Shitov
 
Perl 5.10 и 5.12
Andrew Shitov
 
Say Perl на весь мир
Andrew Shitov
 
Ad

Recently uploaded (20)

PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 

How to clean an array