1
votes

I'm trying to match a regex string against a data file in perl, but it constantly keeps skipping the exact line that I'm heading for... what can possibly be wrong here?

My file says:

<div class="definitionBox details" id="id-udt">
<span class="stempel">Udtale</span>
<span class="tekstmedium allow-glossing">
<span class="lydskrift"><span class="diskret">[</span>be&#712;g&#248;n&#704;&#601;<span class="diskret">]</span></span>
</span>

I'm going for the class "lydskrift" line, so I tried to grab its content in multiple ways until I ended up trying to match just everything like so:

while (<FILE>) {
    if ( <FILE> =~ m/(.+)/ ) {
        open FARA, '>>:encoding(UTF-8)', 'udtale.txt';
        print (FARA $1 . "\n");
        close (FARA);
    }
}

Surprisingly it keeps giving me this:

<div class="definitionBox details" id="id-udt">
<span class="tekstmedium allow-glossing">
</span>

Interestingly enough, it matches all four lines if I put them in a DATA area inside the same perl file! But that's not what I want, so what makes the difference here?

2

2 Answers

4
votes

First of all, I think your file has one more line at the top that you're not including. The reason for my suspicion is below.

Your problem isn't the regex, your problem is that <FILE> reads a line each time you call it. So every run through your loop reads one line in the while(<FILE>) and then another one in the if(<FILE> =~ m/(.+)/). Your if should be just this:

if(m/(.+)/)

so that it uses the default $_ variable that the while(<FILE>) will be populating.

Furthermore, your while loop is doing a lot more work than it has to, you could just do this:

open FARA, '>>:encoding(UTF-8)', 'udtale.txt';
while(<FILE>) {
    print FARA;
}
close (FARA);

or even this:

open FARA, '>>:encoding(UTF-8)', 'udtale.txt';
print FARA while(<FILE>);
close (FARA);

If you're trying to skip blank lines, then maybe this:

open FARA, '>>:encoding(UTF-8)', 'udtale.txt';
while(<FILE>) {
        chomp;
        print FARA $_, "\n" if($_);
}
close (FARA);
2
votes

To build on mu is too short's solution, this is how I'd write it:

open FARA, '>>:encoding(UTF-8)', 'udtale.txt' or die $!;
while (<FILE>) {
        print FARA if /./;
}
close FARA;

or, if you also want to skip lines consisting only of whitespace:

open FARA, '>>:encoding(UTF-8)', 'udtale.txt' or die $!;
while (<FILE>) {
        print FARA if /\S/;
}
close FARA;