Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Think about Loose Coupling
 
PerlMonks  

Re^2: Clubbing array elements together:

by muba (Priest)
on Jan 29, 2013 at 16:19 UTC ( #1015901=note: print w/ replies, xml ) Need Help??


in reply to Re: Clubbing array elements together:
in thread Clubbing array elements together:

In itself, there is nothing wrong with a foreach loop. However, the original specification mentioned double blancs, and neglected to explicitly mention what should happen with a single blanc, but it can be inferred from "b) All elements between a set of double blanks should be combined together into one element" that single blancs should just be added to that one element. Or ignored, which boils down to the same. Your code, however, will act as soon as it find a blanc, no matter whether it's a double or single one:

use Data::Dumper; # Two strings, single blanc, two strings, double blanc my @words=("abcd","efgh", "", "jklm","nopq", "", ""); my $combined; my @combinations; for my $word (@words){ if ($word){ $combined .=$word; } else{ push @combinations, $combined if $combined; $combined=""; } } @words=@combinations; print Dumper\@words;
$VAR1 = [ 'abcdefgh', 'jklmnopq' ];

Here's a little something that almost does what newbie1991 specified. It just doesn't deal well with arrays that don't start with a double blanc: it will just pretend that the array did.

use strict; use warnings; use Data::Dump 'pp'; my @words1 = ("", "", "abcd", "efgh", "", "", "jklm", "nopq", "", "") +; my @words2 = ("", ",", "abcd", "efgh", "", "", "jklm", "nopq", ""); my @words3 = ("", "", "abcd", "efgh", "", "jklm", "nopq", "", "", "rst +u", "vwxy", "", ""); print "Double blancs:\n"; my @combined1 = process(@words1); pp \@combined1; print "\nArray doesn't start or end with double blancs:\n"; my @combined2 = process(@words2); pp \@combined2; print "\nArray contains single blanc:\n"; my @combined3 = process(@words3); pp \@combined3; sub process { my @input = @_; my @output = (); my $buffer = undef; my $last = shift @input; # We take the first el +ement for whatever it is while ( defined(my $this = shift @input) ) { if ($last eq "" and $this eq "") { # Double blancs # When we run into the first double blanc, # $buffer will be undef. # We don't want to push that. push @output, $buffer if defined $buffer; $buffer = ""; } elsif ($this ne "") { # Non-blanc string # ($buffer || "") to prevent "undefined value in concatena +tion" warning $buffer = ($buffer || "") . $this; } $last = $this; } return @output; }
Double blancs: ["abcdefgh", "jklmnopq"] Array doesn't start or end with double blancs: [",abcdefgh"] Array contains single blanc: ["abcdefghjklmnopq", "rstuvwxy"]


Comment on Re^2: Clubbing array elements together:
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-04-21 00:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls