Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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"]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015901]
help
Chatterbox?
[RonW]: Tunnel FTP through stunnel?
[james28909]: i guess i should have done it in perl. i bet it woulnt have taken me 5 hours to figure out haha
[choroba]: In the end, I was able to upgrade Perl from 5.8.3 to 5.22 and install Net::SFTP::Foreign , at least for the task involved
[james28909]: what would be the best way to capture that stream with perl? ffmpeg args -rtp rtp://127.0.0.1?
[LanX]: if I was forced to talk about all security risks I encountered oO
[james28909]: if i did that with ffmpeg, i could then listen on the port with perl right?
[LanX]: one of my clients filtered a menu linking to Web pages according to user rights. .. but he didn't secure the access to those unlisted pages

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2017-05-22 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?