join (@a, @b, @c)

Discussion in 'Perl Misc' started by Ken Sington, Jun 22, 2004.

  1. Ken Sington

    Ken Sington Guest

    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?
     
    Ken Sington, Jun 22, 2004
    #1
    1. Advertising

  2. Ken Sington

    Sam Holden Guest

    On Tue, 22 Jun 2004 03:34:15 -0400,
    Ken Sington <ken_sington@nospam_abcdefg.com> wrote:
    > 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.

    --
    Sam Holden
     
    Sam Holden, Jun 22, 2004
    #2
    1. Advertising

  3. Ken Sington

    Ken Sington Guest

    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?
     
    Ken Sington, Jun 22, 2004
    #3
  4. Ken Sington

    Ken Sington Guest

    Sam Holden wrote:

    > On Tue, 22 Jun 2004 03:34:15 -0400,
    > Ken Sington <ken_sington@nospam_abcdefg.com> wrote:

    .....

    >
    > 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...
     
    Ken Sington, Jun 22, 2004
    #4
  5. Ken Sington

    Anno Siegel Guest

    Ken Sington <ken_sington@nospam_abcdefg.com> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Jun 22, 2004
    #5
  6. Ken Sington

    Sam Holden Guest

    On Tue, 22 Jun 2004 04:01:06 -0400,
    Ken Sington <ken_sington@nospam_abcdefg.com> wrote:
    > 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...]



    --
    Sam Holden
     
    Sam Holden, Jun 22, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd Anderson

    join('')

    Todd Anderson, Sep 11, 2003, in forum: Perl
    Replies:
    1
    Views:
    657
    Kris Wempa
    Sep 12, 2003
  2. laurent
    Replies:
    7
    Views:
    1,941
    Joe Smith
    May 27, 2004
  3. Taewoon Kwon

    Qustion about ithreads join

    Taewoon Kwon, Sep 10, 2004, in forum: Perl
    Replies:
    1
    Views:
    442
    Joe Smith
    Sep 15, 2004
  4. Alan Silver
    Replies:
    0
    Views:
    917
    Alan Silver
    Jun 5, 2006
  5. googleboy
    Replies:
    1
    Views:
    956
    Benji York
    Oct 1, 2005
Loading...

Share This Page