Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: new to perl

by zentara (Archbishop)
on Jul 13, 2012 at 15:12 UTC ( [id://981643]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://981643]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found