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

I have a uni assignment to complete by 1/26/2005 but am struggling with perl as I have had only 1 session. I am running activeperl on windows xp and I need to create a perl script that will print the last line of a text file with a ";" in the line. Also I need to create a batch file that will create a sorted frequency list from a text file but that will include "'"'s and "-"'s as parts of words but not dashes which are represented in the file as "---"? Please help if only to tell me where I could look to find out I can't really make much sense of the "perlre" documentation!

Retitled by davido.

Replies are listed 'Best First'.
Re: homework: Simple matching and sorting
by holli (Abbot) on Jan 24, 2005 at 20:31 UTC
Re: homework: Simple matching and sorting
by CountZero (Bishop) on Jan 24, 2005 at 20:37 UTC
    For the first part of your assignment:
    use strict; my $lastline; while (<DATA>) { $lastline = $_ if /;/; } print $lastline; __DATA__ This is not the right line This is OK, it contains a ; This is no good either Hey, a ; in this line! Drop this.
    Of course you will want to change the <DATA> filehandle for some proper filehandle pointing to the text file.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: homework: Simple matching and sorting
by CountZero (Bishop) on Jan 24, 2005 at 20:45 UTC
    Or even shorter:
    use strict; my @lines=grep(/;/, <DATA>); print pop @lines; __DATA__ This is not the right line This OK, it contains a ; This is no good either Hey, a ; in this line! Drop this.
    It is shorter, but it uses more memory if the text file is large as it reads in all lines of the file and keeps all lines with a ';' only to print the last line.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: homework: Simple matching and sorting
by Anonymous Monk on Jan 25, 2005 at 10:06 UTC
    1. perl -ne '$,=$_if/;/END{print$,}' file.txt
    2. perl -ne '$,{$_}++for/(?:[\x27\w]+|-(?!--))+/g; END{for(sort{$,{$a}<=>$,{$b}}keys(%,)){printf"%4d: %s\n",$,{$_},$_}}' +file.txt
    I didn't test the first one, but I did test the second.
Re: homework: Simple matching and sorting
by DrHyde (Prior) on Jan 25, 2005 at 10:25 UTC
    ++ for at least admitting that this is homework. Your honesty makes a refreshing change from certain other people here.
Re: homework: Simple matching and sorting
by tphyahoo (Vicar) on Jan 25, 2005 at 10:45 UTC
    What's a sorted frequency list?