Hi there
I have a file that looks something like this
Z 5 89 Z 92 102 Z 103 123 Z 126 150
In a nutshell - the length of the 'Z item' corresponds to the numbers following it. For example, the first Z item starts at element 5 and ends at element 89, making it 84 elements in length.
I want to find out 1) how many Z items there are in a given file and 2) the length of each of these Z items. Which would be remarkably easy if it wasn't for the following:
In some cases, a particular Z item has been mistakingly broken up into two. These mistakes are instantly recognisable because the 'stop element' of one incorrectly assigned Z item will be followed instantly by another Z item for which the start element is only one number after the previous stop one.
For example
Z 92 102 Z 103 123
Is in reality only one Z item, yet in the file its recognised as two.
In my perl script I want to count such incorrectly assigned Z items as only being one whereever necessary but I am at a bit of a loss as to how to do that.
I guess I'm looking for a way of 'rewinding' the file so I can compare the stop element of one Z item with the start element of the next one. My present code is as follows.
open(FILE, "$input") || die "OOPS! Can't find file!\n"; while(<FILE>) { @file = split(/\s+/, $_); $zitem = $file[0]; if("$zitem" eq "Z") { $start_element = $file[1]; $stop_element = $file[2]; } }
Any pointers in the right direction much appreciated.

In reply to 'rewinding' file to get value from previous line in file? by Angharad

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.