Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: prepend string to entire array

by gmax (Abbot)
on Sep 10, 2003 at 09:50 UTC ( #290354=note: print w/replies, xml ) Need Help??


in reply to prepend string to entire array

A few solutions from a previous thread with a similar problem.

@result = map scalar reverse, glob("{1,2,3,4,5,6,7}{A,B,C,D,E,F,G}"); # CheeseLord @result=map{$a=$_;map$a.$_,1..7}"A".."G"; # runrig @result=map$_.1..$_.7,"A".."G"; # tilly # which translates, in your case, into @result=map$_.$array1[0]..$_.$array1[$#array1],@array;

Update: extravagant solutions
broquaint complained in the CB that I haven't suggested any solutions based on Tree::DAG_Node. While I invite the curious reader to peek at Variant permutation, I propose here a gratuitously heavy DBI solution. :)

#!/usr/bin/perl -w use strict; use DBI; if (-f "permute") { unlink "permute" or die "can't unlink permute file\n" } my $dbh = DBI->connect("dbi:SQLite:permute", "","",{RaiseError=>1, PrintError=> 0 }) or die "Error in connecton ($DBI::errstr)\n"; my @array = ('A'..'G') ; my @array1 = ( 1 .. 7 ); $dbh->do(qq{CREATE TABLE t1 (a char(1)) }) or die "Error in table creation ($DBI::errstr)\n"; $dbh->do(qq{CREATE TABLE t2 (b char(1)) }) or die "Error in table creation ($DBI::errstr)\n"; $dbh->do("begin"); eval { my $sth = $dbh->prepare (qq{insert into t1 values (?)}); $sth->execute($_) for @array; $sth = $dbh->prepare (qq{insert into t2 values (?)}); $sth->execute($_) for @array1; }; if ($@) { $dbh->do("rollback"); die "error inserting\n"; } else { $dbh->do("commit"); } my $recs = $dbh->selectall_arrayref( qq{select a,b from t1,t2 }) # an infamous CROSS JOIN ! or die "error fetching records\n"; my @results; push @results, join( "", @$_) for @$recs; print "@results\n";
 _  _ _  _  
(_|| | |(_|><
 _|   

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-12-09 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?