Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^5: Nested Data Structures, OOP

by kcott (Chancellor)
on Jul 24, 2012 at 00:07 UTC ( #983263=note: print w/replies, xml ) Need Help??


in reply to Re^4: Nested Data Structures, OOP
in thread Nested Data Structures, OOP

It's good to see you're paying attention to deprecation warnings.

You can use the splice function to remove elements from an array. Here's an example:

$ perl -Mstrict -Mwarnings -E ' my @x = qw{a b c}; say "@x"; splice @x, 1, 1; say "@x"; ' a b c a c

If you know the value you want to delete but not the array index of that value, grep may be a better option:

$ perl -Mstrict -Mwarnings -E ' my @x = qw{a b c}; say "@x"; @x = grep { $_ ne q{b} } @x; say "@x"; ' a b c a c

Also, an empty list will not add elements to the array, e.g.

$ perl -Mstrict -Mwarnings -E ' my @x = ( qw{a}, (), qw{c} ); say "@x"; ' a c

You might use this feature in, for instance, a map statement. This example shows discarding certain values and modifying the remainder:

$ perl -Mstrict -Mwarnings -E ' my @x = qw{a b c}; say "@x"; @x = map { $_ ne q{b} ? uc : () } @x; say "@x"; ' a b c A C

You may also find the following modules useful:

  • List::Util - A selection of general-utility list subroutines.
    This is a builtin module; you should have it already.
  • List::MoreUtils - Provide the stuff missing in List::Util.
    This is a CPAN module; you may need to install it.

-- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983263]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2018-07-21 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (449 votes). Check out past polls.

    Notices?