join (@a, @b, @c)

K

Ken Sington

my @a = (1..5);
my @b = (a..e);
my @c = (v..z);

print join "\n=============\n", ( (join "\n", @a), (join "\n", @b), (join "\n", @c));



function in place of value, cool.

but this gets kind of ugly if you have lots of arrays.

what if I decide to read in a text file? something large.

perhaps theres a more dynamic way to do this?
 
S

Sam Holden

my @a = (1..5);
my @b = (a..e);
my @c = (v..z);

print join "\n=============\n", ( (join "\n", @a), (join "\n", @b), (join "\n", @c));



function in place of value, cool.

but this gets kind of ugly if you have lots of arrays.

what if I decide to read in a text file? something large.

perhaps theres a more dynamic way to do this?

print join "\n=============\n", map {join "\n", @$_} \@a, \@b, \@c;

Though I'm not sure what you are actually asking.
 
K

Ken Sington

or this:

my @a = (1..5);
my @b = (a..e);
my @c = (v..z);
.... (and theoretically)
my @x ...
my @y ...
my @z ...
....
my @aa ...
my @bb ...
my @zz ...

&x(\@a, \@b, \@c...);


sub x {
my @a=@_;
foreach (@a) {
my @b=@{$_};
print join "\n", @b;
print "\n==============\n";
}

}





now I have another problem.
how would I return all that?
 
K

Ken Sington

Sam said:
On Tue, 22 Jun 2004 03:34:15 -0400,


Though I'm not sure what you are actually asking.
well, if I have an unknown number of arrays like @a...@z...@veryBig.
and I want to join each @array.
then join each already joined set of arrays.

so if each item in an array is joined by "\n"
and each array is joined by some kind of separator like "\n=============\n".

so that the output is:
1 <---- item in array @a
2 <---- same here
3 <---- and here
4
5
============== <---- this separates each array
a <---- item in array @b
b
c
d
e
==============
v <---- item in array @c
w
x
y
z
==============
etc...
 
A

Anno Siegel

Ken Sington said:
my @a = (1..5);
my @b = (a..e);
my @c = (v..z);

The last two will fail under stioct.
print join "\n=============\n", ( (join "\n", @a), (join "\n", @b),
(join "\n", @c));



function in place of value, cool.

You are aware that the last line is printed without a line feed?
but this gets kind of ugly if you have lots of arrays.

what if I decide to read in a text file? something large.

perhaps theres a more dynamic way to do this?

print join "\n========\n", map join( "\n", @$_), \ ( @a, @b, @c);

You can also use a ready-made array of arrays instead of the
final " \ ( @a, @b, @c)".

Anno
 
S

Sam Holden

or this:
[snip code]

&x(\@a, \@b, \@c...);

sub x {
my @a=@_;
foreach (@a) {
my @b=@{$_};
print join "\n", @b;
print "\n==============\n";
}

}

now I have another problem.
how would I return all that?

by instead using;

sub x {
my $result = '';
foreach (@_) {
$result .= join "\n", @$_;
$result .= "\n==============\n";
}
return $result;
}

I got rid of the temporaries (@a and @b) since they cause the argument
list and then the elements in each array to get copied, you could also
do something with map (similar to my previous reply), such as:

sub x {
return join '', map {join "\n", @$_,"==============\n"} @_;
}

[Aside: using x as a sub name, for testing purposes, is bad (IMO) since
the obvious next step is a sub named 'y', which leads to madness...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top