nmm_7280 has asked for the wisdom of the Perl Monks concerning the following question:

i do have a array
a[1:0] a[3:2] a[5:4]
and i need the output to be a[5:0], can you please help?? i tried with the hash and it works for the above sequence but wht if i have a[1:0] a[5:4] a[3:2]

Replies are listed 'Best First'.
Re: array question
by Happy-the-monk (Canon) on Feb 02, 2015 at 08:58 UTC

    I find it pretty difficult to make sense of your question - for once, please edit your post and put code tags around <code>the code</code> or c tags around <c>code</c> you use inside sentences.

    In your post there are obviously no sigils $ or @ or % around what looks like your data or part of some code for the array you mention. So I cannot really tell whether you mean to show some data or pseudo code or if something went wrong when you thought you entered perl code. And I don't understand. Better put the real code you have used until you got your unwanted results into your question.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: array question
by roboticus (Chancellor) on Feb 02, 2015 at 08:16 UTC

    nmm_7280:

    If you used a hash, I'd expect it to work in any sequence. So have you tried running it with the different input orders? You didn't post your code, so there's really no way of telling if your code would work with a different input sequence or not.

    If you did it the way I'd've expected, it would work if you entered a[0:0], a[4:5], a[1:3] or a[4:5], a[0:3] as well.

    Why not post your code here so we can take a look at it?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: array question
by kennethk (Abbot) on Feb 02, 2015 at 20:13 UTC
    It may be helpful to review How do I post a question effectively?. There are a few issues that the monks here are having in understanding your question.
    1. The data you have posted does not clearly map to data in memory. I'm guessing that your array initialization code would actually look like:

      my @array = ( 'a[1:0]', 'a[3:2]', 'a[5:4]', );
      but I could be wrong. This is why Anonymous Monk asked you to post your array initialization statement. Because of how you've presented it, my first thought was you has some weird array slice question.
    2. How one maps your dataset to your output is unclear. Again, I could guess at an answer (biggest first number, smallest second number), but that's just a guess. Does every element necessarily look like a[*:*]? You said you "tried with the hash", but it is unclear to me how a hash would solve this issue. Can you post your preliminary code that shows some results?

    If I implemented this (based on some wild guesses), I'd use a regular expression in a for loop and cache the max and min in script-level variables. Or possibly use max and min from List::Util in conjunction with maps (e.g. my ($max) = max map /(\d)/, @list;). But this is largely dictated by your spec, which is poorly defined.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: array question
by Anonymous Monk on Feb 02, 2015 at 07:34 UTC
    Can you please post perl code of array initialization?
Re: array question
by AnomalousMonk (Archbishop) on Feb 03, 2015 at 02:54 UTC

    Are you sure the OP is Perl code? Looks like it might be D.
    Code (D 2.065.0 for Win32):

    import std.stdio; void main () { int[int] a = [ 1:0, 3:2, 5:4, ]; foreach (k, v; a) { writefln("%d => %d", k, v); } }
    Output:
    C:\@Work\DMD2\learn\PM\nmm_7280>pm1115263_1 1 => 0 3 => 2 5 => 4
    But whether this is Perl, D or something else, I, like others, still don't understand how your desired output of  a[5:0] relates to the given initial data.


    Give a man a fish:  <%-(-(-(-<

Re: array question
by BillKSmith (Monsignor) on Feb 02, 2015 at 23:23 UTC
    Two examples is not enough to specify your problem. I will guess that each of your array elements specifies a network connection. Your result is an equivalent connection (possibly the longest path). If so, a search of CPAN modules would be time well spent. A search for the word 'network' returns 260 modules. Initially, only consider modules in your application area. If you are lucky, you will find exactly what you need. If not, your time will probably be repaid in the form of new insight into this type of problem and it solutions.
    Bill
Re: array question
by soonix (Chancellor) on Feb 03, 2015 at 06:59 UTC
Re: array question
by jmmitc06 (Beadle) on Feb 03, 2015 at 02:05 UTC
    Yes, it is unclear what you are wanting, what your problem is? Are you trying to make a 2D array and you want a[5][0] or something?