#!/bin/perl # This snippet keeps track of strings (collections) I've seen, and tests to see if # we've seen this next string. If not, I "remember" it. # # To remember strings and to be able to easily check later if I've seen it, # it's best to use associative arrays like I do here, rather than what I used # to do with Rexx, keeping it in one long, ever-growing string. and using # wordpos to search it. Perl has no wordpos equivalent, forcing me to do my # own delimiting and checking for space-word-space (yuck!). # # The two relevant lines here are $collections_seen{$line} = 1; to set # and if ( $collections_seen{$line} == 1 ) to check. while () { chomp; $line=$_; if ( $collections_seen{$line} == 1 ) { print "I've seen $line before.\n"; } else { print "I haven't seen $line before. I'll add it now.\n"; $collections_seen{$line} = 1; # Remember new string. # print "\%collections_seen is now =>", sort keys %collections_seen, "<\n"; } }