Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Obtaining terms in an expansion

by trammell (Priest)
on Jan 05, 2006 at 23:19 UTC ( [id://521386]=note: print w/replies, xml ) Need Help??


in reply to Obtaining terms in an expansion

Does this do what you want?
my $n = $ARGV[0] || 5; my @terms = qw( a[0][0] a[0][1] ); foreach my $i (1 .. $n-1) { @terms = map { ("$_ a[$i][0]","$_ a[$i][1]") } @terms; } foreach my $i (0 .. $#terms) { print "term[$i] = $terms[$i]\n"; }
Update: fixed off-by-one error

Replies are listed 'Best First'.
Re^2: Obtaining terms in an expansion
by runrig (Abbot) on Jan 05, 2006 at 23:40 UTC
    You have N terms going in, and N terms coming out. He says in the OP that he wants 2^N expressions returned. This is considered 2 terms, so there are 4 expressions returned.

    Update: my mistake. Did not read carefully enough.

      That's funny, because when I run the program for N=2, I get:
      term[0] = a[0][0] a[1][0] a[2][0] term[1] = a[0][0] a[1][0] a[2][1] term[2] = a[0][0] a[1][1] a[2][0] term[3] = a[0][0] a[1][1] a[2][1] term[4] = a[0][1] a[1][0] a[2][0] term[5] = a[0][1] a[1][0] a[2][1] term[6] = a[0][1] a[1][1] a[2][0] term[7] = a[0][1] a[1][1] a[2][1]
      So perhaps I have an off-by-one error, but certainly I'm generating 2^N terms.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2025-07-18 01:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.