in reply to new to perl

I don't know if anyone noticed, but in the sample file given, there was a space between the last word and the bracketed br in the first line, and no space in the second. I don't know if it was a typo, or if it was an intentional fine point. In any event, it screwed up the spilt on space to array, giving 1 extra element.

There may be a clever regex to do this, but here is a simple way a beginner can understand.

#!/usr/bin/perl use warnings; use strict; open (my $fh, "< test.txt") or die "$!\n"; #input file open (my $oh, "> $0-out.txt") or die "$!\n"; #output file my $script = '/home/whoever/bin/myscript.pl'; while (<$fh>){ my $string = $_; # strip off trailing <br> and anything after it $string =~ s/<br>.*$//; #strip whitespace at end in case space preceded the <br> $string =~ s/\s+$//; ## split on space my @words = split / /, $string; #print join "\n",@words,"\n"; my $lastword = $words[-1]; print $oh "$script $lastword\n"; }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: new to perl
by kroz (Initiate) on Jul 16, 2012 at 06:06 UTC

    The bracket was not intentional - i must have missed it when i was writing the post. There is no space or bracket there.