Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Concatenate similar data in array

0 views
Skip to first unread message

Anthony Brooke

unread,
Apr 17, 2008, 12:47:20 AM4/17/08
to beginner perl mailling list
Hello, my logic is really bad, here is I want to do.

my @list = qw(a b a a d e e );

I want to compact the array by concatenating the adjacent vowels and consonant together, like for the above it should become,

my @list2 = qw(ab aa d ee);

How do I get the @list2 ? Thanks.


Send instant messages to your online friends http://uk.messenger.yahoo.com

Jenda Krynicky

unread,
Apr 17, 2008, 9:18:28 AM4/17/08
to beginner perl mailling list
Date sent: Wed, 16 Apr 2008 21:47:20 -0700 (PDT)
From: anthony brooke <esi...@yahoo.com>
Subject: Concatenate similar data in array
To: beginner perl mailling list <begi...@perl.org>

> Hello, my logic is really bad, here is I want to do.
>
> my @list = qw(a b a a d e e );
>
> I want to compact the array by concatenating the adjacent vowels and
> consonant together, like for the above it should become,
>
> my @list2 = qw(ab aa d ee);
>
> How do I get the @list2 ? Thanks.

I think you meant either qw(ab aad ee) or qw(a b aa d ee). In either
case it's easier to start with a string than with an array. Something
like:

@list2 = ('abaadeec' =~ /([aeiouy]*(?:[^aeiouy]|$))/g);
pop(@list2) if $list2[-1] eq '';

or

@list2 = ('abaadeec' =~ /([aeiouy]+|[^aeiouy]+)/g));
pop(@list2) if $list2[-1] eq '';

Depends on what did you actually want, the example you gave makes no
sense to me.

Jenda
===== Je...@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Rob Dixon

unread,
Apr 17, 2008, 12:03:23 PM4/17/08
to beginner perl mailling list, anthony brooke
anthony brooke wrote:
> Hello, my logic is really bad, here is I want to do.
>
> my @list = qw(a b a a d e e );
>
> I want to compact the array by concatenating the adjacent vowels and consonant together, like for the above it should become,
>
> my @list2 = qw(ab aa d ee);
>
> How do I get the @list2 ? Thanks.

The program below appears to do what you need.

HTH,

Rob


use strict;
use warnings;

my @list = qw(a b a a d e e );

my @list2 = syllables(@list);

print "(@list2)\n";


sub syllables {

return @_ unless @_ > 1;

my @return;

foreach (@_) {
if ($return[-1] =~ /[^aeiou]$/) {
push @return, $_;
}
else {
$return[-1].= $_;
}
}

@return;
}

0 new messages