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

I need to cut the first line off from within the file. Here is the code:
#!/usr/bin/perl -w use strict; use LWP::Simple; my $data = get ("http://www.bloomberg.com/energy/index.html"); my ($wanted) = $data =~ /<!-+PETROLEUM-+>\s*(.*)\s*<!-+POWER-+>/s; open (FH,'>../data/file.txt') || die $!; # > creates a new file, >> ap +pends print FH $wanted; close FH; # not really necessary in this simple scrip

Replies are listed 'Best First'.
Re: Help removing first line from retrieved text file.
by mirod (Canon) on Jun 25, 2001 at 10:20 UTC

    you just need to substitute the first line by nothing in $wanted. using .*? instead of .* in you first regexp also improves performances quite dramatically.

    #!/usr/bin/perl -w use strict; use LWP::Simple; my $data = get ("http://www.bloomberg.com/energy/index.html"); my ($wanted) = $data =~ /<!-+PETROLEUM-+>\s*(.*?)\s*<!-+POWER-+>/s; # + the .*? speeds up processing considerably $wanted=~ s/^[^\n]*\n//s; # just remove the first line open (FH,'>test_remline.txt') || die $!; # > creates a new file, >> ap +pends print FH $wanted; close FH; # not really necessary in this simple scrip

    I feel I have to warn you that what you are doing is probably quite risky as you rely on comments in the HTML that Bloomberg can probably change at will, plus of course parsing HTML with regexps is not the best way to do it: you might want to look at HTML::Parser or HTML::TableExtract. And the .* construct leads to numerous problems described in Ovid's famous Death to Dot Star! post (although personally I don't have a problem with .*?).

Re: Help removing first line from retrieved text file.
by lemming (Priest) on Jun 25, 2001 at 10:02 UTC

    Please use Super Search and Search before asking questions.
    You must of missed my CB reference to Deleting the first line of a file.
    As an exercise, use your title in the search box (upper left).

      I saw the code and have updated it as follows. The darned first line is still there.
      #!/usr/bin/perl -w use strict; use LWP::Simple; my $data = get ("http://www.bloomberg.com/energy/index.html"); my ($wanted) = $data =~ /<!-+PETROLEUM-+>\s*(.*)\s*<!-+POWER-+>/s; open (FH,'>../data/file.txt') || die $!; print FH $wanted; close FH; open(FH, '+< ../data/file.txt') || die "Could not open $myfile $!\n"; @bar=<FH>; shift(@bar); seek(FH, 0, 0); truncate(FH, tell); print FH @bar; close(FH);
Re: Help removing first line from retrieved text file.
by Beatnik (Parson) on Jun 25, 2001 at 10:03 UTC
Re: Help removing first line from retrieved text file.
by toma (Vicar) on Jun 25, 2001 at 10:17 UTC
    A nice way to deal with lines of a file in perl is to create an array that holds each line of the file. Then you can process the lines using the very nice perl array manipulation commands, such as  shift, pop, push, splice, join, split, for, and, for the adventurous map.

    Here is an example:

    use strict; my $data="The first line the second line the third line the last line. "; my @lines= split(/\n/, $data); shift @lines; print join "\n", @lines; print "\n";

    It should work perfectly the first time! - toma