Perl Programming

CSE3395




Today's Perl Script


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



My, it IS nice to conceive of a computer program in an instant, and then have it written before you a few hours later, functioning as expected!

I'm beginning to like perl!

#!/usr/bin/perl
#perlWords 1.0 written Wed 12th April 2006 Mathieu Tozer.com
#Keeps track of the words you're reading. Might be useful for language learning.
#WARNING! VERY BASIC FUNCTIONALITY. But it works as far as I can tell : )

#takes as input newTxt.txt and checks against myWords for new words. Adds them if not found.

use Data::Dumper;

# Thaw saved Dictionary, if it exists.
if ( -f "myWords" ) {
    do "myWords";# Restore previous values.
} else {
    %myWordsDict = ( ) ;  # First run.
}

# Print usage message if called with no arguments.
die "Usage: $0 file [...]\n" if @ARGV == 0;

$text = $ARGV[0];
#transient variable for today's words
%todaysWords = ( ) ;
                                # ... do stuff with %myWordsDic ...

#Backquoteoperator used to capture output of another process

@newWords = ;

foreach $current ( @newWords )
{
    if ( exists $myWordsDict { "$current" } ) { #if it's in my dictionary, ( do nothing atm )
#$myWordsDict { "$current" } = ( $myWordsDict { "$current" } + 1 ) #add one to times seen
#print "you have seen $current";
     }
    else { #otherwise add it to my dict and set to 1
        #    print "NEW: $current";
        $myWordsDict { "$current" } = "1";
        #add the word to 'today's words' file with the total number of words
        $todaysWords { "$current" } = $myWordsDict { "$current" } ;
     }
}

print "Today's Words are from $text:\n";
foreach $word ( keys %todaysWords )
{
    print ". $word";
}

# Freeze values for next time before program exit.
open STORAGE, ">myWords";
print STORAGE Data::Dumper->Dump (
                                  [\%myWordsDict],
                                  [qw ( *myWordsDict ) ] ) ;


0 Responses to “Today's Perl Script”

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


Previous posts

Archives

Links