sun@icgeb has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on how do I find one number that matches in elements of same array and combine those elements?

Replies are listed 'Best First'.
Re: how do I find one number that matches in elements of same array and combine those elements?
by jethro (Monsignor) on Jul 31, 2009 at 08:32 UTC

    This is an array of strings:

    @array = ("1 2 3 4 5 6 7 8 10 14 15 20", "7 14 20", "21 22 23 24 40 50");

    This is an array of arrays:

    @array = ([1, 2, 3, 4, 5, 6, 7, 8, 10, 14, 15, 20], [7, 14, 20], [21, 22, 23, 24, 40, 50]);

    This is a syntax error:

    @array = (1 2 3 4 5 6 7 8 10 14 15 20, 7 14 20, 21 22 23 24 40 50);

    It seems the second alternative is what you might be looking for. If yes, you should read perllol and perldsc.

Re: how do I find one number that matches in elements of same array and combine those elements?
by moritz (Cardinal) on Jul 31, 2009 at 06:17 UTC
    @array = (1 2 3 4 5 6 7 8 10 14 15 20, 7 14 20, 21 22 23 24 40 50);

    This is not valid Perl code. Should that be three strings? Or three array references?

    I want to combine the elements

    How do you want to combine them? concatenate? Or sort numerically, and remove duplicates?

    How do I merge two elements if all the elements of one element are present in other as in 0th and first element?

    How are we supposed to know what your requirements are?

    Also, what have you tried so far?

    Please read How (Not) To Ask A Question.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how do I find one number that matches in elements of same array and combine those elements?
by CountZero (Bishop) on Jul 31, 2009 at 06:18 UTC
    1. Your question is not clear to me, perhaps you can give an example of the output you expect from your program based upon the @array you showed.
    2. What have you tried already?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how do I find one number that matches in elements of same array and combine those elements?
by Anonymous Monk on Jul 31, 2009 at 06:15 UTC
    I have an array like

    Please post code that compiles (and put it in code tags), so we can know exactly what you have, without guessing.

Re: how do I find one number that matches in elements of same array and combine those elements?
by ssandv (Hermit) on Jul 31, 2009 at 15:10 UTC
    Nodes are written in html. Please use <br> tags to put some line breaks in so it's easier to read.

    As to your problem (which I'm not completely sure I understand, but I think I have some idea what you're asking), if you want to combine multiple lists, remove duplicates, and sort the result, you're looking for a hash. If you make a hash key from each list element, the duplicates will automagically resolve themselves, and then you can
    sort keys %whatever_you_name_your_hash
    to get the sorted result back as a list. Edit: fixed and noted.