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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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"]

In reply to Re^2: Clubbing array elements together: by muba
in thread Clubbing array elements together: by newbie1991

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (8)
    As of 2018-07-17 09:22 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 (361 votes). Check out past polls.

      Notices?