Appending two arrays horizontally

Discussion in 'Perl Misc' started by Edward wijaya, Aug 7, 2004.

  1. Hi,

    I have this two arrays:

    @arr1 = qw(3 2 2 1);
    @arr2 = qw(cc dd ff gg);

    is there any way I can append this two arrays
    so that it becomes

    @arr3 = ["3 cc", "2 dd", "3 ff", "1 gg"];

    Thanks before hand

    Regards
    Edward WIJAYA
    SINGAPORE
     
    Edward wijaya, Aug 7, 2004
    #1
    1. Advertising

  2. Edward wijaya <> writes:

    > Hi,
    >
    > I have this two arrays:
    >
    > @arr1 = qw(3 2 2 1);
    > @arr2 = qw(cc dd ff gg);
    >
    > is there any way I can append this two arrays
    > so that it becomes
    >
    > @arr3 = ["3 cc", "2 dd", "3 ff", "1 gg"];


    Assuming you know in advance they're both the same size:

    my @arr3 = map { join(" ",$arr1[$_],$arr2[$_]) } (0..$#arr1);

    This loops from 0 to the last element in @arr1, and for each number
    joins the appropriate array elements together with a space.

    ----ScottG.
     
    Scott W Gifford, Aug 7, 2004
    #2
    1. Advertising

  3. Edward wijaya

    Dave Cross Guest

    On Sat, 07 Aug 2004 16:00:57 +0800, Edward wijaya wrote:

    > Hi,
    >
    > I have this two arrays:
    >
    > @arr1 = qw(3 2 2 1);
    > @arr2 = qw(cc dd ff gg);
    >
    > is there any way I can append this two arrays
    > so that it becomes
    >
    > @arr3 = ["3 cc", "2 dd", "3 ff", "1 gg"];


    @arr3 = map { "$arr1[$_] $arr2[$_}" } 0 .. $#arr1

    This assumes that you know the arrays are of equal length.

    Dave...
     
    Dave Cross, Aug 7, 2004
    #3
  4. On 07 Aug 2004 04:09:22 -0400, Scott W Gifford <>
    wrote:

    > my @arr3 = map { join(" ",$arr1[$_],$arr2[$_]) } (0..$#arr1);


    Well, I'm not a big fan of useless quoting, but

    my @arr3 = map "$arr1[$_],$arr2[$_]", 0..$#arr1;

    is more clear IMHO.

    >Assuming you know in advance they're both the same size:


    Also, not difficult to cope with this:

    my @arr3 = do {
    no warnings 'uninitialized';
    map "$arr1[$_] $arr2[$_]",
    0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
    };

    (implicitly assuming C<use warnings;> as it "Should"!)


    PS: anser if you like, but then take into account that most probably I
    won't be able to read news for one or two weeks or possibly even till
    September!


    Michele
    --
    $\=q.,.,$_=q.print' ,\g,,( w,a'c'e'h,,map{$_-=qif/g/;chr
    }107..q[..117,q)[map+hex,split//,join' ,2B,, w$ECDF078D3'
    F9'5F3014$,$,];];$\.=$/,s,q,32,g,s,g,112,g,y,' , q,,eval;
     
    Michele Dondi, Aug 9, 2004
    #4
  5. On Sat, 07 Aug 2004 16:00:57 +0800, Edward wijaya
    <> wrote:

    >@arr1 = qw(3 2 2 1);
    >@arr2 = qw(cc dd ff gg);
    >
    >is there any way I can append this two arrays
    >so that it becomes
    >
    >@arr3 = ["3 cc", "2 dd", "3 ff", "1 gg"];


    Isn't the C<zip> operator supposed to handle exactly this kind of
    situations? Oh, but were you still talking Perl5?!? ;-)


    PS: anser if you like, but then take into account that most probably I
    won't be able to read news for one or two weeks or possibly even till
    September!


    Michele
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Aug 9, 2004
    #5
  6. Edward wijaya

    David Combs Guest

    In article <>,
    Michele Dondi <> wrote:
    >On 07 Aug 2004 04:09:22 -0400, Scott W Gifford <>
    >wrote:
    >
    >> my @arr3 = map { join(" ",$arr1[$_],$arr2[$_]) } (0..$#arr1);

    >
    >Well, I'm not a big fan of useless quoting, but
    >
    > my @arr3 = map "$arr1[$_],$arr2[$_]", 0..$#arr1;
    >
    >is more clear IMHO.
    >
    >>Assuming you know in advance they're both the same size:

    >
    >Also, not difficult to cope with this:
    >
    > my @arr3 = do {
    > no warnings 'uninitialized';
    > map "$arr1[$_] $arr2[$_]",
    > 0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
    > };
    >
    >(implicitly assuming C<use warnings;> as it "Should"!)


    Why the no-warnings stmt?

    In case one of the arrays is uninitialized?

    Or in case one of the values *within* one of the
    arrays is?



    Maybe I've been blind, but this is the first
    time I've seen a no-warnings used in this kind
    of situation. (Well, at least it's rarely seen
    in this group's code-pieces.)

    Anything to say about whether to do this
    in general?

    Also -- *why* turn off the warnings, ie why
    don't you *want* to know about them (these "errors"?)?

    Thanks!

    David
     
    David Combs, Aug 31, 2004
    #6
  7. Edward wijaya

    Anno Siegel Guest

    David Combs <> wrote in comp.lang.perl.misc:
    > In article <>,
    > Michele Dondi <> wrote:
    > >On 07 Aug 2004 04:09:22 -0400, Scott W Gifford <>
    > >wrote:
    > >
    > >> my @arr3 = map { join(" ",$arr1[$_],$arr2[$_]) } (0..$#arr1);

    > >
    > >Well, I'm not a big fan of useless quoting, but
    > >
    > > my @arr3 = map "$arr1[$_],$arr2[$_]", 0..$#arr1;
    > >
    > >is more clear IMHO.
    > >
    > >>Assuming you know in advance they're both the same size:

    > >
    > >Also, not difficult to cope with this:
    > >
    > > my @arr3 = do {
    > > no warnings 'uninitialized';
    > > map "$arr1[$_] $arr2[$_]",
    > > 0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
    > > };
    > >
    > >(implicitly assuming C<use warnings;> as it "Should"!)

    >
    > Why the no-warnings stmt?
    >
    > In case one of the arrays is uninitialized?


    No. An uninitialized array is never a problem, it behaves like an
    empty array. The same goes for hashes, it's only scalars where
    the distinction makes a difference.

    > Or in case one of the values *within* one of the
    > arrays is?


    Possibly that, but mainly to catch undefined values that come from
    *outside* one of the arrays.

    The arrays will in general have different lengths and the index runs
    over the longer of the two. So the shorter array will be probed beyond
    its end and return undefs. The main purpose of "no warnings ..." is
    to catch those.

    Anno
     
    Anno Siegel, Aug 31, 2004
    #7
  8. On Tue, 31 Aug 2004 04:54:29 +0000 (UTC), (David
    Combs) wrote:

    >> my @arr3 = do {
    >> no warnings 'uninitialized';
    >> map "$arr1[$_] $arr2[$_]",
    >> 0 .. ($#arr1>$#arr2 ? $#arr1 : $#arr2);
    >> };
    >>
    >>(implicitly assuming C<use warnings;> as it "Should"!)

    >
    >Why the no-warnings stmt?


    Not to get (one of those rare authentically) *unwanted* warnings.

    >In case one of the arrays is uninitialized?
    >
    >Or in case one of the values *within* one of the
    >arrays is?


    The latter, with the former as a special case of it.

    >Anything to say about whether to do this
    >in general?


    In general there are situations in which it is desirable or even
    advisable to *locally* disable some warnings or strictures.

    >Also -- *why* turn off the warnings, ie why
    >don't you *want* to know about them (these "errors"?)?


    Because I *do* know in advance that I may get a warning that is not
    really something going wrong.

    In (my) practice C<no warnings 'uninitialized'> is the most frequently
    used and the most reasonable one.

    Hope this example clarifies things up (I moved the code to a sub):


    #!/usr/bin/perl -l

    use strict;
    use warnings;

    sub zip (\@\@) {
    my @a=@{ $_[0] };
    my @b=@{ $_[1] };
    # no warnings 'uninitialized';
    map "$a[$_] $b[$_]",
    0 .. ($#a>$#b ? $#a : $#b);
    }

    print '@arr1 and @arr2 have the same length';
    my @arr1 = qw /foo bar baz/;
    my @arr2 = 0 .. $#arr1;
    print for (zip @arr1, @arr2), '';

    print '@arr1 and @arr2 have different lengths';
    @arr2 = 0 .. @arr1;
    print for zip @arr1, @arr2;

    __END__


    If you run it, then you get:


    # ./foo.pl
    @arr1 and @arr2 have the same length
    foo 0
    bar 1
    baz 2

    @arr1 and @arr2 have different lengths
    Use of uninitialized value in concatenation (.) or string at
    ../foo.pl line 10.
    foo 0
    bar 1
    baz 2
    3


    If you uncomment the <no warnings> line the warning goes away and you
    get the (supposedly) desired output. Of course it is implicit that I
    made an educated guess at what the desired output is for undefined
    entries...


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, Aug 31, 2004
    #8
  9. Edward wijaya

    David Combs Guest

    Thanks to both of you.

    I'll give the example a try.

    Thanks again!

    David
     
    David Combs, Sep 2, 2004
    #9
    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. JSOUL
    Replies:
    3
    Views:
    474
    Barbara de Zoete
    Nov 9, 2005
  2. Nathan Gilbert

    Appending rows to 2-dim arrays

    Nathan Gilbert, Dec 7, 2005, in forum: C Programming
    Replies:
    1
    Views:
    283
    Michael Mair
    Dec 7, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,134
    Philipp
    Jan 20, 2009
  4. Kev Jackson
    Replies:
    2
    Views:
    114
  5. Adam Akhtar
    Replies:
    5
    Views:
    652
    Adam Akhtar
    Mar 25, 2008
Loading...

Share This Page