Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Manipulating Arrays

by webchalkboard (Scribe)
on Aug 11, 2005 at 11:42 UTC ( [id://482912]=perlquestion: print w/replies, xml ) Need Help??

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

Hi again,

I have a much simplier and cooler question this time, and was wondering the best way to do this.

Basically I have an array of words for example ('the','cat','sat','on','the','matt') and I want to create lots of different combinations of these words, so I would like to end up with short two word phrases like 'cat matt' and 'cat sat' or 'matt cat the'.

What is the best way of achieving this result? I was thinking about counting the number of items in the array and then connecting random elements together. Most phrases would only be about 4 words long, and if I could get every permutation of these words up to say 50 permutations that would be perfect... i'm sure there is a cool perl way, any suggestions?

Many thanks,
Tom

Learning without thought is labor lost; thought without learning is perilous. - Confucius
WebChalkboard.com | For the love of art...

Replies are listed 'Best First'.
Re: Manipulating Arrays
by Taulmarill (Deacon) on Aug 11, 2005 at 11:57 UTC
    you should have a look at the Algorythm::Permute module on CPAN.
Re: Manipulating Arrays
by davidrw (Prior) on Aug 11, 2005 at 12:28 UTC
Re: Manipulating Arrays
by Limbic~Region (Chancellor) on Aug 11, 2005 at 12:37 UTC
    webchalkboard,
    Ok, you speak of combinations and of permutations. They are not the same thing. In a nutshell, a combination doesn't not consider a group of elements different if the only thing that changes is the position while a permutation does. So do you want combinations or permutations?

    Additionally, it looks like you want both a fixed size (you mention 2 word phrases and then 4 word long phrases) and a fixed total (50). Do you have unspoken requirements - if so what are they.

    I have code (and so do other monks and CPAN) that does combinations, permutations, fixed size groupings, etc. I need more information before I can recommend one of them.

    Cheers - L~R

    Update: I did have a double negative pointed out by AM below. The correction is to strike one of them as order doesn't matter in combinations. The attempted correction by AM is incorrect.

      a combination doesn't not consider a group of elements different if the only thing that changes is the position while a permutation does

      Confusion alert! Confusion alert! That sentence is extremely hard to understand, even though I know what you're trying to say. I think one of the most egregious problems is the double negative, doesn't not. For the sake of others, I'll try to translate:

      The ordering of the elements matters for a combination, but doesn't matter for a permutation.
Re: Manipulating Arrays
by chester (Hermit) on Aug 11, 2005 at 13:18 UTC
Re: Manipulating Arrays
by salva (Canon) on Aug 11, 2005 at 12:50 UTC
Re: Manipulating Arrays
by webchalkboard (Scribe) on Aug 11, 2005 at 13:11 UTC

    Thanks for all the hints, enough to keep me going for a while i'm sure :)

    Learning without thought is labor lost; thought without learning is perilous. - Confucius
    WebChalkboard.com | For the love of art...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-18 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found