in reply to Faster Hash Slices

I have a program that reads and processes approximately 8 million lines of data. For each line read it does:

my @aa = split(/\t/); @{$ref}{@head} = @aa;

Well, sorry, but that program does seem to make any sense. As far as we can say from the code you presented, this program is just assigning 8 million times the split line to the same HoH element (nothing is ever changing $ref or @head). If this is what you want to do, then simply go to the last line of the file and assign its split value to your variable. But, of course, I suspect you want to to something else, but are not really saying what.

Please explain what you really want. Or, stated differently, please provide a more comprehensive excerpt of your code.

Replies are listed 'Best First'.
Re^2: Faster Hash Slices
by davido (Cardinal) on Nov 29, 2013 at 05:34 UTC

    @{$ref}{@head} = @aa;

    ...is a hash slice, similar to @hash{@keys} = @values, except that in the former case, $ref holds a hash-ref, whereas in the latter case, %hash holds a plain hash.


    Dave

      I might not have expressed myself correctly, but I was only trying to say that, since $ref and @head are never changing in the presented code, each time through the loop, the values of @aa will clobber the previously recorded ones, so that the loop is in effect useless, it would be faster to go straight to the last line of the file, split it and run the discussed command only once. Example under the Perl debugger:

      DB<1> @head = qw 'foo bar baz' DB<2> @{$ref}{@head} = qw /one two three/; DB<3> x $ref 0 HASH(0x8032f9b8) 'bar' => 'two' 'baz' => 'three' 'foo' => 'one' DB<4> @{$ref}{@head} = qw /four five six/; DB<5> x $ref 0 HASH(0x8032f9b8) 'bar' => 'five' 'baz' => 'six' 'foo' => 'four' DB<6>
      The assignment of line 4 is clobbering what was previously in the hash referenced by $ref. It does not make sense to do that in a loop, since only the last assignment of the loop is useful.

      Or the real code is different, in which case it might be useful to see the real code.

        so that the loop is in effect useless, it would be faster to go straight to the last line of the file, split it and run the discussed command only once.

        What "discussed command"? The OP doesn't mention any command; nor show any "command".

        You appear to be assuming that the op reads all the lines of the file; and then runs this "discussed command" once the rad loop ends; but there is no evidence of that within the thread.

        An alternative scenario: He reads a line; splits it to the hash; accesses a few named fields and performs some calculation that affects some variables declared before the loop is entered; and then loops back and reads the next line; until the file is read.

        There is no evidence from the OP one way or the other anywhere in the thread, but to assume your version is to assume the OP is ...


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.