in reply to Re: Alternative to Substr?
in thread Alternative to Substr?

Hi,

Thanks for this.

I am trying to read out the 6 characters following "DosID" in a longer string into a new variable ($dosid). Here is the code using the position. As the position is not always the same (sometimes it starts at 81, sometimes at 44, etc.), I have to use another if-clause to correct the value. This can get rather bothersome, if the position jumps around all the time.

foreach $input (@input) { if (($input=~'DosID=') && ($count=~/[02468]$/)) { $count2++; $dosid=substr($input,81,6); if ($dosid=~'detai') { $dosid=substr($input,44,6); } if ($dosid=~'"') { chop($dosid); } }
The two count variables ($count, $count2) are counting the lines of the input file and the number of lines containing the search string "DosID" respectively. I am only interested in every second occurence.

I tried it with $', but this seems to pick up the value of one of the count variables instead of the remainder of the string.

Another thing: I would like not having to specify the number of characters of the substring. Instead I would like to use a delimiter as a stop signal (in my example: a " character should delimit the substring). I could use a loop checking every character and then appending it to my variable, but I thought there might be a more elegant way of doing things.

I guess I am looking for a function like substr which includes regular expressions and if-clauses instead of fixed parameters. Thus, the start of the substring would be defined as "begin after" and the end as "stop before".

Replies are listed 'Best First'.
Re^3: Alternative to Substr?
by davorg (Chancellor) on Sep 29, 2004 at 12:25 UTC

    You should really take another (closer) look at perlre (or perhaps perlretut). Particularly the bits about using the match operator (m//).

    my $count = 0; foreach (@input) { if (/DosID(.{6})/ and $count++ % 2) { $dosid = $1; } }

    Of course, this overwrites the value of $dosid each time it finds a match, so you might need to change the logic a bit.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      There is one serious bug in this code and a second less serious bug.

      If a line does not contain DosID= then the short circuit means that the increment on $count never occurs so the skip alternation will breakdown
      The original logic looked at even index elements, this looks a odd ones
        If a line does not contain DosID= then the short circuit means that the increment on $count never occurs so the skip alternation will breakdown

        But the original poster said "I am only interested in every second occurence" so I deliberately wrote it that way - counting occurrences _not_ lines.

        The original logic looked at even index elements, this looks a odd ones

        Yep. There's a difference. Mine looks at "every second occurence" as requested (i.e. it skips the first and displays the second and so on). If that's wrong then you fix it by changing the postincrement to preincrement.

        And why did you hide your text?

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re^3: Alternative to Substr?
by tachyon (Chancellor) on Sep 29, 2004 at 12:31 UTC

    You are making hard work of it. Modulus % is good for skipping N items and you can increment at the same time. You have $count and $count2 FWIW -> use strict to get told the error of your ways by Perl.

    my $count = 0; for my $input (@input) { next unless $count++ % 2 == 0; print "Got $1\n" if $input =~ m/DosID=(.{6})/; }