Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'm try to get the min value and position in array1. Then get the value in the same position from array 2.
#!usr/bin/perl use Tkx; use Tkx::Pane; use Cwd; use strict; use warnings; use List::Util 'max'; use List::Util 'min'; use Statistics::Descriptive 'mindex'; my @array1 = (1,2,3,4,5,6,7); my @array2 = ("a","b","c","d","e","f","g"); my $min = min(@array1); my $min_index = Statistics::Descriptive::Sparse->new(); $min_index -> mindex(@array1); print "$min\n"; print "$min_index\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: min and mindex
by rjt (Curate) on Nov 06, 2012 at 17:28 UTC | |
This does scan @array1 twice, but that will not be an issue for small arrays, and if you are using the XS version of List::MoreUtils, this implementation may still be faster than rolling your own min/mindex code. | [reply] [d/l] [select] |
|
Re: min and mindex
by Kenosis (Priest) on Nov 06, 2012 at 17:37 UTC | |
Given your one-to-one mapping between the two arrays (i.e., the same number of elements) and the numbers in @array1 are not repeated, consider the following:
Output:
Two hashes are created. The first (%array1Hash) pairs each element of @array1 with its position in the array. The second (%hash) pairs the elements of @array1 as the keys with the elements of @array2 as values. When you've found the minimum value in @array1, you can use it as the key to get the associated value from @array2. Hope this helps! Update: Added printing the min val of @array1 and that element's position in the array. | [reply] [d/l] [select] |
by Anonymous Monk on Nov 06, 2012 at 18:15 UTC | |
| [reply] |
by Kenosis (Priest) on Nov 06, 2012 at 19:07 UTC | |
Perhaps something like the following:
Output:
| [reply] [d/l] [select] |
|
Re: min and mindex
by Khen1950fx (Canon) on Nov 06, 2012 at 19:13 UTC | |
| [reply] [d/l] |
|
Re: min and mindex
by kcott (Archbishop) on Nov 06, 2012 at 22:06 UTC | |
The first problem here is that you haven't asked a question! Perhaps ... Please read the guidelines in: How do I post a question effectively? The code you've provided outputs:
Presumably you want:
Checking the documentation for Statistics::Descriptive, you can achieve this by changing
to
See also ++Khen1950fx's response (above) which has similar code changes. I see a number of alternative ways of doing this (without using Statistics::Descriptive) have already been provided. Here's another one that doesn't use any modules at all and handles non-unique values and arrays of uneven lengths.
Output:
You may find Benchmark useful for comparing the alternative solutions supplied. -- Ken | [reply] [d/l] [select] |
|
Re: min and mindex
by tinita (Parson) on Nov 07, 2012 at 00:03 UTC | |
| [reply] [d/l] |
|
Re: min and mindex
by jwkrahn (Abbot) on Nov 07, 2012 at 05:11 UTC | |
I'm try to get the min value and position in array1. You could use a simple loop:
| [reply] [d/l] |
|
Re: min and mindex
by jaredor (Priest) on Nov 08, 2012 at 08:28 UTC | |
Y'all are messing with my PerlMonks drinking game: No one said Schwartzian transform, e.g.,
| [reply] [d/l] |
by kcott (Archbishop) on Nov 09, 2012 at 06:13 UTC | |
++ I like your thinking, jaredor! So, what's the rules of this drinking game? No doubt I lost points when I failed to utter the incantantion "Schwartzian transform" while coding:
OK, I've downed a large Scotch. Your turn. :-) -- Ken | [reply] [d/l] |
by jaredor (Priest) on Nov 10, 2012 at 03:02 UTC | |
Wait, what?! *I* don't get to drink because you didn't say the magic words, but *you* do? I need to rethink the rules to this game.... ... but then it is a technical fault that I didn't grok that at the core of your masterful schooling on question posting, problem analysis and solution testing was The Schwartzian ... so okay, I owe you a point or a pint, whichever you prefer. Actually, you caught me in a minor sin, since I think "reduce" (++tinita) would be what I would use for the problem as stated (sort of like spinning a log on a lathe to get a toothpick). So I posted an answer which wasn't a great answer for the question, but allowed me to be a wee bit cheeky for my own amusement. The "drinking game" is just something I do to keep myself engaged with the site content and is just a variant of what some people do to keep awake in meetings: See if you can predict a response. Successful prediction? Assess whether it was necessary or gratuitous. Gratuitous? Point! Take a drink. For PM my rubric is (Now you see why I felt compelled to add working code to my snarky comment.) This game is not for everyone: According to a thread I read a while back, sundialsvc4 would have destroyed BrowserUK's liver years ago. ;-) | [reply] |