Friday, 15 February 2013

Changing value of Array perl -


OK, so I'm scratching my head here.

I am looking to retrieve the highest value from the array in the class value, and then repeat that value and save it in the array. My argument was asked to write the following piece of code:

  #! / Usr / bin / perl Usage list: UTL qw (minimum max); MyArray = ({Cloth => "Autumn", Price => 40, Quantity => 2}, {Clothes => "Socks", Value => 5, Quantity => 12} ,); My $ maxi = max (@array); $ Name = $ {$ maxi} {value}; $ Name = $ name * 2; Print $ name;  

Now, to find this code, it seems that it randomly toggles between two values. How will I get the maximum value from the described category and how can I get new values ​​and replace it with the old?

max

You can remove prices from structures: < / P>

  my @prices = map $ _- & gt; {Value}, @array; My $ max = max (@presentations);  

To find the same structure, use:

  my @maximal = grep $ _- & gt; {Value} == $ max, @array; $ _- & gt; {Value} * = 2 = for max;  

(I used an array, because there may be more than one such element).

Or, do not use max , the circle on your own array:

  my @max_idx = 0; My $ i (for $ 1 # $ array) {my $ cmp = $ array [$ i] {price} & lt; = & Gt; $ Array [$ max_idx [0]] {value}; If ($ cmp> = 0) {@max_idx = () if $ cmp; Push @max_idx, $ i; }} $ _- & gt; {Value} * = 2 for @array [@max_idx];  

No comments:

Post a Comment