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

I am working on a little script that will check the value of field X and increment the value of field Y by 1 based on the number of times the value of X has come up. My first though was to simply create and array and push unique X values to it and Set Y to 1 when that happens...if it were not unique and equaled a value already in the array I would add 1 to Y. Simple enough....if only all instances of X were in consecutive, which they are not.

My question is how to accomplish this? I just can't seem to wrap my brain around it (even though it seems trivial). I am still very new to perl and sometimes the obvious just is not obvious to me =P

  • Comment on How to increment a sequence number for evert instance of a value?

Replies are listed 'Best First'.
Re: How to increment a sequence number for evert instance of a value?
by Tomte (Priest) on Jun 25, 2003 at 20:29 UTC

    Use the values ox X as keys in a hash and increment the hash-value

    my $x; my %y = (); while( $x = getvaluesofx() ) { $y{$x} += 1; }
    just an example, not working code :)

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: How to increment a sequence number for evert instance of a value?
by aschroh (Novice) on Jun 25, 2003 at 21:23 UTC
    Someone had asked me if this was homework....wasn't sure how to respond as it was in the chatter box as a private message (just read the faq...so now I do). This is not homework =) That would require tuition and time off from work he he he. I am learning perl the "hard way".
      aschroh

      Tomte answered your question, you can also "perldoc -q unique keys" Perldoc is great, but sometimes it's difficult to think of the key words you are looking for. (if you knew them, you probably wouldn't have to look them up). I have found it's best when just starting out to ask were to look rather than for the answer directly, it shows you want to learn instead of having someone do your HW.
        I agree, I wasn't looking so much for a "here it is use it". I just was lost and not sure how to even approach this. I did a lot of reading on hashes and updating values in hashes last night and now I understand what he was referring to. I appreciate everyones input =)
Re: How to increment a sequence number for evert instance of a value?
by chip (Curate) on Jun 25, 2003 at 20:29 UTC
    You haven't explained what you're actually doing yet. What's a "field"? What are X and Y?

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      My appologies in an attempt to be brief and to the point...I have made my question too vague. This is what I am doing. X one of many ID numbers. I want to loop through my file and write it to a second file checking the ID number on the way. For every UN-unique instance of the ID (say it appear 10 times throughout the file)I want to increment it's sequence number So the output might look like this ID SEQ 1 1 1 2 2 1 3 1 1 3 My problem is I just am not good enough to understand how to accomplish this unless all ID 1's were consecutive and all ID 2's were consecutive etc etc etc. aschroh
        would help if I used the code tags gees sorry about that
        ID SEQ 1 1 1 2 3 1 2 1 1 3 2 2
        This is what I want it to output. The problem is the ID aren't consecutive in my file. The question is how to write this so it looks at the ID and KNOWS 1) if it's the first time it's come up and 2) if it's not the first time what to set the SEQ number to?
Re: How to increment a sequence number for evert instance of a value?
by aschroh (Novice) on Jun 26, 2003 at 15:33 UTC
    Ok this is what I came up with....it "looks" right to me. I do a Print " The ID is $rp1 and the Sequence is $invseq\n"; and the ID prints but the sequence has no value. I guess I messed up in the hash? Not really sure what I am doing wrong now.
    if ($flag21 == 1) { foreach $num(@number) { if (($num) eq ($rp1)) { $invseq = $seqi{$rp1}; $invseq = $invseq + 1; $seqi{$rp1} .= $invseq; } else { $seqi{$rp1} .= 1; $invseq = $seqi{$rp1}; push @number, $rp1; } } }
    I changed the first part to a 3 step add thinking I messed up somewhere there it was originally this (and didn't work)
    if ($flag21 == 1) { foreach $num(@number) { if (($num) eq ($rp1)) { $seqi{$rp1} += 1; $invseq = $seqi{$rp1}; } else { snipped the rest off as it was the same
      Ok guys Enlil explained Tomte example in terms even I can understand =P. I got it working correctly now....and it was only 3 lines instead of 20 lol. Thanks to all who responded here and in Chatterbox =)