# stringlist - full functionality list of strings class package StringList; use Data::Dumper; $Data::Dumper::Indent = 0; sub new { my($class) = @_; bless({stringlist => [],}, $class); } sub push { my($self,$item) = @_; my($list) = $self->{stringlist}; push(@$list,@$item); } sub size { my($self) = @_; my($list) = $self->{stringlist}; return scalar(@$list); } sub at { my($self,$i) = @_; my($list) = $self->{stringlist}; return $list->[$i]; } sub stringify { my($self) = @_; my($list) = $self->{stringlist}; return Dumper($list); } sub clear { my($self) = @_; @{$self->{stringlist}} = (); } sub elimdups { # eliminates duplicates from a list (aka make a set, make a list of unique items) # (note: was called uniquify...a momentary slip in taste) my($self) = @_; my($list1) = $self->{stringlist}; my($hash1) = hashify($list1); my($list2) = listify($hash1); my($list3) = StringList->new(); $list3->push($list2); return $list3; } sub sameitems { my($self,$stringlist2) = @_; my($hash1,$item); my($list1) = $self->{stringlist}; my($list2) = $stringlist2->{stringlist}; $hash1 = hashify($list1); foreach $item (@$list2) { if (!(exists($hash1->{$item}))) { return(0); } } return(1); } sub hashify { # hashifies a list: (a,b) -> (a => 1,b => 1) my($list) = @_; my(%hash,@list2,$val); foreach $val (@$list) { push(@list2,$val); push(@list2,'1'); } %hash = @list2; return \%hash; } sub listify { # listifies a hash: (a => 1,b => 1) -> (a,b) my($hash) = @_; my(@list,$key); @list = (); foreach $key (keys %$hash) { push(@list,$key); } return \@list; } sub printit { my($listname1,$listname2,$equal) = @_; if ($equal) { print "Same Lists: ", $listname1, " and ", $listname2, "\n"; } else { print "Different Lists: ", $listname1, " and ", $listname2, "\n"; } } #sub sort {} #sub unique {} #sub push_file { } package main; print "\nlist1:\n"; $list1 = StringList->new(); $toadd = ['a','b','b','c','d','d','e','f','f']; $list1->push($toadd); $list3 = StringList->new(); $toadd = ['d','e','f','g','h','i']; $list3->push($toadd); $size = $list1->size(); print "size: $size\n"; for ($i = 0; $i < $size; $i++) { $item = $list1->at($i); print "item $i: $item\n"; } print "\n"; print "list1: ", $list1->stringify(), "\n"; $list2 = $list1->elimdups(); print "list2: ", $list2->stringify(), "\n"; print "list3: ", $list3->stringify(), "\n"; StringList::printit("list1","list2",$list1->sameitems($list2)); StringList::printit("list1","list3",$list1->sameitems($list3)); print "\nClearing out list1:\n"; $list1->clear(); print "list1: ", $list1->stringify(), "\n"; exit;