After reading your initial post a few times and looking at the responses and your followups, it seems to me that your real problem here is that you don't understand your problem well enough to explain it thoroughly.

The two closest things you gave us to a spec were the following descriptons:

  • If the index difference of two consecutive sample is 1 take the first colum of the first string PLUS the 2nd column - last one- of the last string.
  • If the difference of two consecutive sample is >1 take all the colum in between
If the index difference of two consecutive elements of the array is 1 take the 1st colum of all the array i.e $sample->[0] .. $sample->[-1] PLUS the 2nd column of the last element of the array $sample->[-1].
Both of which seem to suffer from a lack of clarity. It might help you to describe the problem better if you try to be precise with the language you use. Saying things like 'PLUS' when you don't actually mean addition could be counterproductive here. Also, your "samples" are strings but you talk about their "difference" as an integer. Presumably, you mean the difference of what you are calling their "indexes" but that's ambiguous because they are in an array and array elements already have an "index". You are also using "last" ambiguously. Are you referring to the last string in the array or the last of the two you've taken? (I think you tried to clear that up, but are you done at that point or what?) And, you are talking about things like "first column of the first string" but your code actually turns those strings into arrays at the outset so your description fails to match your code.

The first thing I'd do is abstract away the format of your data in your presentation of the problem. It doesn't matter whether they are whitespace separated substrings, hashes, multidimensional arrays, or objects with member variables. As it is, your code quickly turns the strings into mulidimensional arrays anyway. That's fine but if you are going to do that, it might be helpful to use constants for the indices. (They'll be easier to remember than 0, 1, and 2.) Alternatively, use a hash and name the columns. There's a little more overhead but it might make for clearer code. You can always change it later if it's actually an issue. The important thing is to get a handle on what you are doing.

Secondly, I'd change my way of labeling the columns, in particular I would call your third column something other than "index" to avoid the term overlapping with Perl's notion of "index". Maybe "position" or "id" would work. I don't know how to rename "col1" and "col2" but, as is, they don't visually stand out from each other. I'll use 'X' and 'Y' instead but expect you could come up with something better.

Putting that together, I'd try to start explaining the problem with a data structure that looked more like this...

$sample = [ { X => 'str1', Y => 'str2', position => 1 }, X => 'str3', Y => 'str4', position => 2 }, X => 'str5', Y => 'str6', position => 3 }, X => 'str7', Y => 'str8', position => 4 }, X => 'str9', Y => 'str10', position => 5 } ];
And that would allow you to be very specific in your explanation of the problem.

You could start by saying something like... "I need a function which will take an array reference like $sample shown above and which will return an array reference like $result" (here filling in what $result looks like.) And then you can fill in the details about how you need to construct $result by referring to the actual sample data, $sample, that you provided.

In other words, you'll be able to refer to values like $sample->[0]{position} - $sample[1]{position} and you'll be able to say things like push $sample[0]{X} onto @$result without there being any question what you actually mean.

If you take the time to describe your problem clearly, you might very well figure it out on your own. And, if it is still giving you trouble, it will be much easier for others to get a handle on what you want and provide you with advice. I, for one, have no idea what you are trying to accomplish because I see a lot of ambiguity and a general lack of clarity in the way you've presented your problem and I haven't seen much improvement in your follow-ups.

† Note the similarity to the word specification.

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Simple Logic Problem with Perl by sauoq
in thread Simple Logic Problem with Perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.