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

by hash (Scribe)
on Jul 31, 2003 at 18:36 UTC ( #279711=sourcecode: print w/replies, xml ) Need Help??
Category: text processing
Author/Contact Info hash
Description: Code usefull to edit files automaticly removing the last "." in the selected string lines.
# Get files and skip "." in the selected strings.
# jeffa is right. :)
# Usage: ./killlastdot <ENTER> file word

my $FILE = <STDIN> ;
chop $FILE ;
my $VAR = <STDIN> ;
chop $VAR ;
open(GETARQ, "$FILE") ;
my @ARQ = <GETARQ> ;
close(GETARQ) ;
        foreach (@ARQ) {
                if ($_=~/$VAR/) {
                        my $x = $_ ;
                        chop($x) ;
                        chop($x) ;
                        print "$x\n" ;
        print $_ ;
Replies are listed 'Best First'.
(jeffa) Re:
by jeffa (Bishop) on Aug 01, 2003 at 02:27 UTC
    Since this program removes any trailing dot on lines that contain some particular string, i don't see why you named it ... but that's okay. Here is some code that does the same thing as yours, but does so more consisely:
    #!/usr/bin/perl use strict; use warnings; my ($file,$var) = @ARGV; die "USAGE: $0 file word\n" unless $file and $var; open GETARG, '<', $file or die "Can't open $file: $!\n"; while (<GETARG>) { s/\.$// if /$var/; print; } close GETARG or die "Couldn't close $file: $!\n";
    Also, if you don't mind "hard coding" the word you want to match in the code, this can be trimmed down to a one liner: (remember to replace the double quotes with single quotes if you use Win32)
    perl -pe's/\.$// if /word/' file
    Perl rules. :)


    (the triplet paradiddle with high-hat)
      Also, if you don't mind "hard coding" the word you want to match in the code, this can be trimmed down to a one liner

      Nevermind "hard coding" it... and on Unix, you might as well replace it with an alias. Here's how I'd do that in my shell (bash):

      alias rmdots="perl -pe 's/\.\$// if /@{[shift]}/'"
      Uh... (test first!) Make that:
      alias rmdots="perl -pe 'BEGIN{\$p=shift}s/\.$// if /\$p/'"
      Usage would be: rmdots pattern file ...

      "My two cents aren't worth a dime.";
      perl -pi -e's/(word.*)\.$/$1/' file

      Makeshifts last the longest.

      jeffa, your code is much more consisely, becouse it shows error messages if the user try a wrong way to input file and word. But it works for me. Anyway i would call this script as "one-way script" becouse i used for an especific usage, that i should remove dots from a text file in SQL.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://279711]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2020-10-21 02:00 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (212 votes). Check out past polls.