I am 100% new to Perl but do have some PHP knowledge. I'm trying to create a quick script that will take the @url vars and save it to a .txt file. The problem that I'm having is that it's saving the url again everytime it runs through the loop which is super annoying. So when the loop runs, it'll look like this.
for each line { add the line to @urls; print the whole @urls array }
Of course you end up with
url1
url1 url2
url1 url2 url3
The simplest thing were to move the printing outside the loop.
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $file = "data.rdf.u8";
my @urls;
open(my $fh, "<", $file) or die "Unable to open $file\n";
while (my $line = <$fh>) {
if ($line =~ m/<(?:ExternalPage about|link r:resource)="([^\"]+)"\/?>/) {
push @urls, $1;
}
}
open (FH, ">>my_urls.txt") or die "$!";
print FH "@urls ";
close(FH);
close $fh;
In that case, you can probably just open it for writing (>) not for appending (>>). But that depends on whether you want to preserve what there was before running the script or not.
Bookmarks