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

Using "tie:file" is there a way of outputting the record numbers where a substitution was successful from the readme
$n_recs = @array; gives total number of records-- but I need the specific record numbers where
for (@array) { s/PERL/Perl/g; }
--was successful
Many thanx
Smbs

20061123 Janitored by Corion: Added code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: tie:file get record number
by johngg (Canon) on Nov 18, 2006 at 11:31 UTC
    You could do something like (not tested)

    for my $recNo ( 0 .. $#array ) { next unless $array[$recNo] =~ m{PERL}; my $changeCt = $array[$recNo] =~ s{PERL}{Perl}g; if ( $changeCt ) { print qq{$changeCt changes in record $recNo\n}; } else { warn qq{Failed to change record $recNo\n}; } }

    I hope this is of use.

    Cheers,

    JohnGG

Re: tie:file get record number
by Not_a_Number (Prior) on Nov 18, 2006 at 11:34 UTC

    Update: Ignore, I completely misread the OP's requirements

    my $count = 0; for ( @array ) { $count += s/PERL/Perl/g; } print $count;

    Update 2: This seems more like what you want:

    my @numbers; for ( 0 .. $#array ) { push @numbers, $_ if $array[$_] =~ s/PERL/Perl/g; } print join ', ', @numbers;

    Update 3: ...which, I see now, actually adds very little value to johngg's post above :-)

      Many thanx works great
      Smbs