Search Results

Search found 3659 results on 147 pages for 'sorted hash'.

Page 13/147 | < Previous Page | 9 10 11 12 13 14 15 16 17 18 19 20  | Next Page >

  • Turning a nested hash structure into a non-nested hash structure - is this the cleanest way to do it

    - by knorv
    Assume a nested hash structure %old_hash .. my %old_hash; $old_hash{"foo"}{"bar"}{"zonk"} = "hello"; .. which we want to "flatten" (sorry if that's the wrong terminology!) to a non-nested hash using the sub &flatten(...) so that .. my %h = &flatten(\%old_hash); die unless($h{"zonk"} eq "hello"); The following definition of &flatten(...) does the trick: sub flatten { my $hashref = shift; my %hash; my %i = %{$hashref}; foreach my $ii (keys(%i)) { my %j = %{$i{$ii}}; foreach my $jj (keys(%j)) { my %k = %{$j{$jj}}; foreach my $kk (keys(%k)) { my $value = $k{$kk}; $hash{$kk} = $value; } } } return %hash; } While the code given works it is not very readable or clean. My question is two-fold: In what ways does the given code not correspond to modern Perl best practices? Be harsh! :-) How would you clean it up?

    Read the article

  • Sort by values from hash table - Ruby

    - by Adnan
    Hello, I have the following hash of countries; COUNTRIES = { 'Albania' => 'AL', 'Austria' => 'AT', 'Belgium' => 'BE', 'Bulgaria' => 'BG', ..... } Now when I output the hash the values are not ordered alphabetically AL, AT, BE, BG ....but rather in a nonsense order (at least for me) How can I output the hash having the values ordered alphabetically?

    Read the article

  • [Haskell]Curious about the Hash Table problem

    - by astamatto
    I read that hash tables in haskell are crippled ( citation: http://flyingfrogblog.blogspot.com/2009/04/more-on-haskells-hash-table-problems.html ) and since i like haskell it worried me. Since the blog-post one year has passed and im curious, The hash table problem in haskell was "fixed" in the traditional compilers? (like ghc) ps: I love stack overflow, im a long time visitor but only today i decided to try to post a question.

    Read the article

  • Cleaning up code - flatten a nested hash structure

    - by knorv
    The following Perl sub flattens a nested hash structure: sub flatten { my $hashref = shift; my %hash; my %i = %{$hashref}; foreach my $ii (keys(%i)) { my %j = %{$i{$ii}}; foreach my $jj (keys(%j)) { my %k = %{$j{$jj}}; foreach my $kk (keys(%k)) { my $value = $k{$kk}; $hash{$kk} = $value; } } } return %hash; } While the code works it is not very readable or clean. My question is two-fold: In what ways does it not correspond to modern Perl best practices? How would you clean it up?

    Read the article

  • Combine hash values in C#

    - by Chris
    I'm creating a generic object collection class and need to implement a Hash function. I can obviously (and easily!) get the hash values for each object but was looking for the 'correct' way to combine them to avoid any issues. Does just adding, xoring or any basic operation harm the quality of the hash or am I going to have to do something like getting the objects as bytes, combining them and then hashing that? Cheers in advance

    Read the article

  • How can I marshal a hash with arrays?

    - by tuner
    What should I do to marshal an hash of arrays? The following code only prints {}. s = Hash.new s.default = Array.new s[0] << "Tigger" s[7] << "Ruth" s[7] << "Puuh" data = Marshal.dump(s) ls = Marshal.restore( data ) p ls If the hash doesn't contain an array it is restored properly.

    Read the article

  • Hash value in the manifest file.

    - by satishtech
    Hello, Im tring to create a manifest for my own.dll, i took the manifest file from C:\WINDOWS\WinSxS\Manifests for example. In that, below tag was one of the line.<file name="msvcr90.dll" hashalg="SHA1" hash="e0dcdcbfcb452747da530fae6b000d47c8674671"> In above tag, hash value was assigned with 40 character. Here comes my doubt, 1) hash value was auto generated, if not, whats it points to?

    Read the article

  • How do I access a value of a nested Perl hash?

    - by st
    I am new to Perl and I have a problem that's very simple but I cannot find the answer when consulting my Perl book. When printing the result of Dumper($request); I get the following result: $VAR1 = bless( { '_protocol' => 'HTTP/1.1', '_content' => '', '_uri' => bless( do{\(my $o = 'http://myawesomeserver.org:8081/counter/')}, 'URI::http' ), '_headers' => bless( { 'user-agent' => 'Mozilla/5.0 (X11; U; Linux i686; en; rv:1.9.0.4) Gecko/20080528 Epiphany/2.22 Firefox/3.0', 'connection' => 'keep-alive', 'cache-control' => 'max-age=0', 'keep-alive' => '300', 'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'accept-language' => 'en-us,en;q=0.5', 'accept-encoding' => 'gzip,deflate', 'host' => 'localhost:8081', 'accept-charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7' }, 'HTTP::Headers' ), '_method' => 'GET', '_handle' => bless( \*Symbol::GEN0, 'FileHandle' ) }, 'HTTP::Server::Simple::Dispatched::Request' ); How can I access the values of '_method' ('GET') or of 'host' ('localhost:8081'). I know that's an easy question, but Perl is somewhat cryptic at the beginning.

    Read the article

  • Collision free hash function for a specific data structure

    - by Max
    Is it possible to create collision free hash function for a data structure with specific properties. The datastructure is int[][][] It contains no duplicates The range of integers that are contained in it is defined. Let's say it's 0..1000, the maximal integer is definitely not greater than 10000. Big problem is that this hash function should also be very fast. Is there a way to create such a hash function? Maybe at run time depending on the integer range?

    Read the article

  • in ruby, how do you make this nested hash work?

    - by David
    this one creates an error: @settings = { :tab1 => { :name => { :required => true }, :description } } need to change :descrpition to :description = {}, but i don't have any values for :description so i want it to remain as is (without the empty = {}) Would you show me the best way to handle this kind of situation? thanks in advance

    Read the article

  • How can I merge several hashes into one hash in Perl?

    - by Nick
    In Perl, how do I get this: $VAR1 = { '999' => { '998' => [ '908', '906', '0', '998', '907' ] } }; $VAR1 = { '999' => { '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] } }; $VAR1 = { '999' => { '996' => [] } }; $VAR1 = { '999' => { '995' => [] } }; $VAR1 = { '999' => { '994' => [] } }; $VAR1 = { '999' => { '993' => [] } }; $VAR1 = { '999' => { '997' => [ '986', '987', '990', '984', '989', '988' ] } }; $VAR1 = { '995' => { '101' => [] } }; $VAR1 = { '995' => { '102' => [] } }; $VAR1 = { '995' => { '103' => [] } }; $VAR1 = { '995' => { '104' => [] } }; $VAR1 = { '995' => { '105' => [] } }; $VAR1 = { '995' => { '106' => [] } }; $VAR1 = { '995' => { '107' => [] } }; $VAR1 = { '994' => { '910' => [] } }; $VAR1 = { '993' => { '909' => [] } }; $VAR1 = { '993' => { '904' => [] } }; $VAR1 = { '994' => { '985' => [] } }; $VAR1 = { '994' => { '983' => [] } }; $VAR1 = { '993' => { '902' => [] } }; $VAR1 = { '999' => { '992' => [ '905' ] } }; to this: $VAR1 = { '999:' => [ { '992' => [ '905' ] }, { '993' => [ { '909' => [] }, { '904' => [] }, { '902' => [] } ] }, { '994' => [ { '910' => [] }, { '985' => [] }, { '983' => [] } ] }, { '995' => [ { '101' => [] }, { '102' => [] }, { '103' => [] }, { '104' => [] }, { '105' => [] }, { '106' => [] }, { '107' => [] } ] }, { '996' => [] }, { '997' => [ '986', '987', '990', '984', '989', '988' ] }, { '998' => [ '908', '906', '0', '998', '907' ] }, { '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] } ]};

    Read the article

  • Calculate sum of objects for each unique object property in Ruby

    - by macek
    I was helping with an answer in this question and it sparked a question of my own. Pie is an object that has a pieces array made of of PiePiece objects. Each PiePiece has a flavor attribute How do I create a hash that looks like this: # flavor => number of pieces { :cherry => 3 :apple => 1 :strawberry => 2 } This works, but I think it could be improved def inventory hash = {} pieces.each do |p| hash[p.flavor] ||= 0 hash[p.flavor] += 1 end hash end Any ideas?

    Read the article

  • Merging k sorted linked lists - analysis

    - by Kotti
    Hi! I am thinking about different solutions for one problem. Assume we have K sorted linked lists and we are merging them into one. All these lists together have N elements. The well known solution is to use priority queue and pop / push first elements from every lists and I can understand why it takes O(N log K) time. But let's take a look at another approach. Suppose we have some MERGE_LISTS(LIST1, LIST2) procedure, that merges two sorted lists and it would take O(T1 + T2) time, where T1 and T2 stand for LIST1 and LIST2 sizes. What we do now generally means pairing these lists and merging them pair-by-pair (if the number is odd, last list, for example, could be ignored at first steps). This generally means we have to make the following "tree" of merge operations: N1, N2, N3... stand for LIST1, LIST2, LIST3 sizes O(N1 + N2) + O(N3 + N4) + O(N5 + N6) + ... O(N1 + N2 + N3 + N4) + O(N5 + N6 + N7 + N8) + ... O(N1 + N2 + N3 + N4 + .... + NK) It looks obvious that there will be log(K) of these rows, each of them implementing O(N) operations, so time for MERGE(LIST1, LIST2, ... , LISTK) operation would actually equal O(N log K). My friend told me (two days ago) it would take O(K N) time. So, the question is - did I f%ck up somewhere or is he actually wrong about this? And if I am right, why doesn't this 'divide&conquer' approach can't be used instead of priority queue approach?

    Read the article

  • Using a Javascript Variable & Sending to JSON

    - by D Franks
    Hello all! I'm trying to take a URL's hash value, send it through a function, turn that value into an object, but ultimately send the value to JSON. I have the following setup: function content(cur){ var mycur = $H(cur); var pars = "p="+mycur.toJSON(); new Ajax.Updater('my_box', 'test.php', { parameters: pars }); } function update(){ if(window.location.hash.length > 0){ content(window.location.hash.substr(1)); // Everything after the '#' } } var curHashVal = window.location.hash; window.onload = function(){ setInterval(function(){ if(curHashVal != window.location.hash){ update(); curHashVal = window.location.hash; } },1); } But for some reason, I can't seem to get the right JSON output. It will either return as a very large object (1:"{",2:"k") or not return at all. I doubt that it is impossible to accomplish, but I've exhausted most of the ways I can think of. Other ways I've tried were "{" + cur + "}" as well as cur.toObject(), however, none seemed to get the job done. Thanks for the help!

    Read the article

  • Efficient way to maintain a sorted list of access counts in Python

    - by David
    Let's say I have a list of objects. (All together now: "I have a list of objects.") In the web application I'm writing, each time a request comes in, I pick out up to one of these objects according to unspecified criteria and use it to handle the request. Basically like this: def handle_request(req): for h in handlers: if h.handles(req): return h return None Assuming the order of the objects in the list is unimportant, I can cut down on unnecessary iterations by keeping the list sorted such that the most frequently used (or perhaps most recently used) objects are at the front. I know this isn't something to be concerned about - it'll make only a miniscule, undetectable difference in the app's execution time - but debugging the rest of the code is driving me crazy and I need a distraction :) so I'm asking out of curiosity: what is the most efficient way to maintain the list in sorted order, descending, by the number of times each handler is chosen? The obvious solution is to make handlers a list of (count, handler) pairs, and each time a handler is chosen, increment the count and resort the list. def handle_request(req): for h in handlers[:]: if h[1].handles(req): h[0] += 1 handlers.sort(reverse=True) return h[1] return None But since there's only ever going to be at most one element out of order, and I know which one it is, it seems like some sort of optimization should be possible. Is there something in the standard library, perhaps, that is especially well-suited to this task? Or some other data structure? (Even if it's not implemented in Python) Or should/could I be doing something completely different?

    Read the article

  • Sorted queue with dropping out elements

    - by ffriend
    I have a list of jobs and queue of workers waiting for these jobs. All the jobs are the same, but workers are different and sorted by their ability to perform the job. That is, first person can do this job best of all, second does it just a little bit worse and so on. Job is always assigned to the person with the highest skills from those who are free at that moment. When person is assigned a job, he drops out of the queue for some time. But when he is done, he gets back to his position. So, for example, at some moment in time worker queue looks like: [x, x, .83, x, .7, .63, .55, .54, .48, ...] where x's stand for missing workers and numbers show skill level of left workers. When there's a new job, it is assigned to 3rd worker as the one with highest skill of available workers. So next moment queue looks like: [x, x, x, x, .7, .63, .55, .54, .48, ...] Let's say, that at this moment worker #2 finishes his job and gets back to the list: [x, .91, x, x, .7, .63, .55, .54, .48, ...] I hope the process is completely clear now. My question is what algorithm and data structure to use to implement quick search and deletion of worker and insertion back to his position. For the moment the best approach I can see is to use Fibonacci heap that have amortized O(log n) for deleting minimal element (assigning job and deleting worker from queue) and O(1) for inserting him back, which is pretty good. But is there even better algorithm / data structure that possibly take into account the fact that elements are already sorted and only drop of the queue from time to time?

    Read the article

  • How to list rpm packages/subpackages sorted by total size

    - by smci
    Looking for an easy way to postprocess rpm -q output so it reports the total size of all subpackages matching a regexp, e.g. see the aspell* example below. (Short of scripting it with Python/PERL/awk, which is the next step) (Motivation: I'm trying to remove a few Gb of unnecessary packages from a CentOS install, so I'm trying to track down things that are a) large b) unnecessary and c) not dependencies of anything useful like gnome. Ultimately I want to pipe the ouput through sort -n to what the space hogs are, before doing rpm -e) My reporting command looks like [1]: cat unwanted | xargs rpm -q --qf '%9.{size} %{name}\n' > unwanted.size and here's just one example where I'd like to see rpm's total for all aspell* subpackages: root# rpm -q --qf '%9.{size} %{name}\n' `rpm -qa | grep aspell` 1040974 aspell 16417158 aspell-es 4862676 aspell-sv 4334067 aspell-en 23329116 aspell-fr 13075210 aspell-de 39342410 aspell-it 8655094 aspell-ca 62267635 aspell-cs 16714477 aspell-da 17579484 aspell-el 10625591 aspell-no 60719347 aspell-pl 12907088 aspell-pt 8007946 aspell-nl 9425163 aspell-cy Three extra nice-to-have things: list the dependencies/depending packages of each group (so I can figure out the uninstall order) Also, if you could group them by package group, that would be totally neat. Human-readable size units like 'M'/'G' (like ls -h does). Can be done with regexp and rounding on the size field. Footnote: I'm surprised up2date and yum don't add this sort of intelligence. Ideally you would want to see a tree of group-package-subpackage, with rolled-up sizes. Footnote 2: I see yum erase aspell* does actually produce this summary - but not in a query command. [1] where unwanted.txt is a textfile of unnecessary packages obtained by diffing the output of: yum list installed | sed -e 's/\..*//g' > installed.txt diff --suppress-common-lines centos4_minimal.txt installed.txt | grep '>' and centos4_minimal.txt came from the Google doc given by that helpful blogger.

    Read the article

  • Winamp question: I want to search through my songs by which playlists they're sorted into

    - by Daddy Warbox
    I'm trying to think of a way to do this. I sort my songs into a variety of playlists corresponding to different 'moods' I might have, but some songs fit for more than one kind of mood (e.g. a techno song might be 'stylish' and 'surreal', or something to that effect). I also give them different star ratings for a general sort of opinion about them. I want to be able to filter my media library by moods I want or don't want, as well as by star rating. Anyone have a good way to do something like this? Alternatively, is there a way to 'tag' my songs by mood and then search up by those tags? because then I could just dump the queried results into playlists as needed. Thanks in advance.

    Read the article

  • JQuery tablesorter - Keeping grouped subheaders together, but still sorted

    - by hfidgen
    Hiya, I'm not really a Javascript programmer, so I'm struggling with this! I'm using the tablesorter plugin along with the Tablegroup plugin, which work very nicely to group the table rows by a parent, and then sort the parents. My problem is though, that I'd also like the child rows to be sorted whilst within the parent group I've done my best to get this working but I'm afraid I've hit a wall. Can anyone suggest a new starter for 10? The example below is working fine - There are 2x groups here: Nordics (Norway and Denmark) DACH (Germany and Austria) If I click on the header row, the groups are sorted, but the child rows within the group are not sorted. <script type="text/javascript"> $(document).ready(function () { $(".tablesorter") .tablesorter({ // set default sort column sortList: [[0,0]], // don't sort by first column headers: {0: {sorter: false}} , onRenderHeader: function (){ this.wrapInner("<span></span>"); } , debug: true }) }); </script> <table id="results-header" class="grid tablesorter table-header" cellpadding="0" cellspacing="0" border="0"> <thead> <tr class="title"> <th class="countries">&nbsp;</th> <th>% market share</th> <th>% increase in mkt share</th> <th>Target achieved</th> <th>% targets</th> <th>% sales inc. M-o-M</th> <th>% sales inc. M-o-M for country</th> <th>% training</th> </tr> </thead> <tbody> <tr id="Nord" class="collapsible parent parent-even collapsed"> <td class="countries">Nordics</td> <td>39.5</td> <td>49</td> <td>69.8</td> <td>51.8</td> <td>43</td> <td>42.5</td> <td>38</td> </tr> <tr id="row-Norway" class="expand-child child child-Nord"> <td class="countries">Norway</td> <td>6</td> <td>45</td> <td>101</td> <td>10</td> <td>20</td> <td>40</td> <td>30</td> </tr> <tr id="row-Denmark" class="expand-child child child-Nord"> <td class="countries">Denmark</td> <td>10</td> <td>20</td> <td>3</td> <td>40</td> <td>50</td> <td>25</td> <td>8</td> </tr> <tr id="DACH" class="collapsible parent parent-odd collapsed"> <td class="countries">DACH</td> <td>77</td> <td>61</td> <td>43</td> <td>98</td> <td>65</td> <td>92.5</td> <td>59.5</td> </tr> <tr id="row-Germany" class="expand-child child child-DACH"> <td class="countries">Germany</td> <td>56</td> <td>24</td> <td>84</td> <td>98</td> <td>32</td> <td>87</td> <td>21</td> </tr> <tr id="row-Austria" class="expand-child child child-DACH"> <td class="countries">Austria</td> <td>98</td> <td>98</td> <td>2</td> <td>98</td> <td>98</td> <td>98</td> <td>98</td> </tr> </tbody> </table>

    Read the article

< Previous Page | 9 10 11 12 13 14 15 16 17 18 19 20  | Next Page >