You won't be able to determine the intended output until you have read the entire input file into memory and have gone over all the records to check the numeric values associated with names in the fourth field. Maybe something like this:
#!/usr/bin/perl use strict; use warnings; my @records; my %max; while (<DATA>) { my @fields = split; my ( $number, $name ) = ( $fields[3] =~ /^(\d+),(\w+)/ ); if ( ! $number or ! $name ) { warn "Line $.: unexpected input ignored: $_"; next; } $max{$name} = $number unless ( exists( $max{$name} ) and $max{$name} >= $number ); push @records, { name => $name, data => $_ }; } for my $rec ( @records ) { my $name = $rec->{name}; my $max = $max{$name}; my $flag = ( $max == 1 ) ? 'single' : 'end'; $rec->{data} =~ s/(\s)$max,$name,/$1$flag,$name,/; print $rec->{data}; } __DATA__ 62556 63635 y 1,andrew,JJ113954 63868 63897 h 1,morgan,JJ113955 68766 69005 j 2,morgan,JJ113955 81099 81630 y 1,flintoff,JJ113952 126185 126699 s 1,austin,JJ113956 135356 135449 3 1,peter,JJ113952 135588 136297 8 2,peter,JJ113952 158146 158367 i 3,peter,JJ113952 441474 441538 b 9,catherine,JJ029490 442666 442843 9 8,catherine,JJ029490 445778 445905 0 7,catherine,JJ029490 446059 446273 l 6,catherine,JJ029490 450319 450379 f 5,catherine,JJ029490

(updated to add a condition for skipping lines that don't match /\d+,\w+/ in fourth field-- it pays to be careful...)

In the for loop, the $rec->{data} =~ s/.../.../ will do nothing on the lines that don't contain the max value for a given name, because the regex won't match. For lines that contain a max value, the replacement will be either "single" or "end" depending on what the max value is.

In order to read from a file name that you put on the command line (i.e. in @ARGV) when you run the script, just remove DATA from while(<DATA>) (and leave off the __DATA__ section at the end) -- there's no need to explicitly open a file whose name is provided via @ARGV.


In reply to Re: Tagging the last elements by graff
in thread Tagging the last elements by crochunter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.