Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983263]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2018-10-15 19:32 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (82 votes). Check out past polls.