perl6: Array; get rid of empty slot (Any)

  • A+
Category:Languages

For an Array containing only Str elements, I can use grep(Str) in order to eliminate empty slot following a :delete;

For example:

my @prov_cd = <AB BC MB NB NL NS ON PE QC SK>;  @prov_cd[2]:delete;                              # Manitoba deleted  @prov_cd;                                        # [AB BC (Any) NB NL NS ON PE QC SK]  my @prov_cd_cleanup = @prov_cd.grep(Str);        # get rid of (Any) empty slot: [AB BC NB NL NS ON PE QC SK]  @prov_cd = @prov_cd_cleanup;                     # [AB BC NB NL NS ON PE QC SK] 

An Array can contain a variety of object types; I would prefer to "grep" everything that is not (Any).

How can I do this ?

Thank you.

 


First, note that if you remove entries via splice instead of :delete, the items will be shifted and no 'holes' will be generate.

Now, if you truly want to filter out just Any, you may do so via

@prov_cd.grep(* !=== Any) 

However, I suspect you're looking for

@prov_cd.grep(*.defined) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: