Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Array slice vs foreach and push

by chrisburel (Initiate)
on Nov 10, 2011 at 01:58 UTC ( [id://937262]=perlquestion: print w/replies, xml ) Need Help??

chrisburel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

There is a real-world application for this, but I'm going to make it simple for the sake of the example.

Let's say I have an array of characters:

 my @chars = qw( a b c ? : ! f g )

And I have a function called "getUpperCaseChars" that converts those characters to their uppercase equivalents. But for the punctuation, since it has no uppercase equivalent, it returns an undef for those indices instead:

my @upChars = getUpperCaseChars( @chars ); # @upChars is now ( 'A', 'B', 'C', undef, undef, undef, 'F', 'G' )

Now after I've called getUpperCaseChars(), I can determine which ones did not have uppercase equivalents by doing something like this:

my @charsWithNoUpperCase = @chars[grep { !defined @upChars[$_] } 0..$#upChars];

But readability is low on that, so I've changed it to a foreach:

my @charsWithNoUpperCase foreach my $index ( 0..$#upChars ) { push @charsWithNoUpperCase, $chars[$index] if !defined $upChars[$index]; }

So my question is, is using the array slice significantly more efficient?

Replies are listed 'Best First'.
Re: Array slice vs foreach and push
by ww (Archbishop) on Nov 10, 2011 at 02:20 UTC
    Not an answer to your splice question, but this seems a good deal more straightforward... if it fits your "realworld application."
    #!/usr/bin/perl use Modern::Perl; # 937262 my @chars = qw( a b c ? : ! f g ); my @newchars = (); for my $char(@chars) { if ($char =~ /[[:alpha:]]/ ) { # posix: letters. cf alts for + unicode "Properties" usage push @newchars, uc($char); }else{ push @newchars, $char; } } for my $newchar(@newchars) { print $newchar . " "; # A B C ? : ! F G }

      Unfortunately, that doesn't fit the real world application. I'll give a bit more context.

      What I have is an array of objects. Each of these objects have a version property. Each object also has a few associated special versions, like one called the official version. Then there's a method that takes an array of these objects and returns an array of these special versions, called getOfficialVersions. But not all objects have an official version, so in that case the method returns an undef for that index in the array. So I'm looking for a quick way to get the list of objects from the input list that have an undef in the array returned from getOfficialVersions.

        if that case, perhaps you should be thinking about using some aspect of ...
         if (defined)
Re: Array slice vs foreach and push
by NetWallah (Canon) on Nov 10, 2011 at 06:41 UTC
    This should meet your readability and efficiency criteria :
    use strict; use warnings; my @chars = qw( a b c ? : ! f g ) ; my %upchars ; @upchars{@chars} = ( 'A', 'B', 'C', undef, undef, undef, 'F', 'G' ); +# getUpperCaseChars( @chars ); my @charsWithNoUpperCase = grep {! defined $upchars{$_} } @chars;

                "XML is like violence: if it doesn't solve your problem, use more."

Re: Array slice vs foreach and push
by ashish.kvarma (Monk) on Nov 10, 2011 at 04:49 UTC

    You can check it yourself using Benchmark. I couldn't help right now with a working example (working on a system with no perl installed), but I am sure you will be able to benchmark your code with help of the documentation (You can always come back for help on Benchmark if you come across any issues).

    If you get the answer using Benchmark, please do share it.

    Regards,
    Ashish
Re: Array slice vs foreach and push
by vinian (Beadle) on Nov 10, 2011 at 11:00 UTC
    perl -w -le 'my @chars = qw( a b c ? : ! f g ); my @up_chars = map { tr/a-z/A-Z/; $_ } @chars; print "@up_chars"'

    hope this would help.

      my @up_chars = map { tr/a-z/A-Z/; $_ } @chars; can be reduced to: my @up_chars = map uc, @chars;

        nice

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://937262]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found