PDA

View Full Version : Perl Sets



danbaron
25-10-2010, 04:52
[font=courier new][size=8pt]Here is a Perl program which demonstrates a function that determines if a passed string qualifies as a valid thinBasic variable name.

It is the Perl equivalent of these two previous programs.

Python --> http://community.thinbasic.com/index.php?topic=3720.msg27313;topicseen#msg27313

Ruby --> http://community.thinbasic.com/index.php?topic=3721.msg27316;topicseen#msg27316

Perl has no set type. But, the keys in a hash can be used as a set. Duplicate hash keys are not permitted, just like duplicate members are not permitted in a
set. The program uses three Perl hashes as sets, (%firstchars, %midchars, %lastchars). The hash values are not used, so, they are arbitrarily set to, 1.

My experience is that Perl is more flexible than Python or Ruby. And, I think that with flexibility, comes complexity. There have been a lot of books written
trying to explain all of the aspects of Perl. So far, I think Python and Ruby are simpler languages. So, in Python and Ruby, I think it is easier to determine
how to do a particular task, because, usually, there is one obvious way to do it. In Perl, there might be lots of ways to do the same thing. So, especially for
the beginner, it can be difficult to find "any" way to do something, because, there are many ways to do it. Since the language is so flexible, obvious solutions
are not always apparent, you may need more of an overall understanding of the language, before any solutions present themselves. I think that is why many people
become frustrated with Perl, and migrate to languages which are less flexible, but simpler (and more consistent).

:oops: :x :twisted:
Dan


#---------------------------------------------------------------------------------------------------
# file = "varnames.pl"
# date: 2010-10-24
#---------------------------------------------------------------------------------------------------

# This is a Perl script that determines which in a list of identifiers (strings) are valid thinBasic variable names.

# If you download it, change its name from, "varnames.pl.txt", to, "varnames.pl".

# To run it, you need Perl installed on your computer.

# To run it, you can open a command window in the folder that contains the file
# (shift + right click on the folder, "Open command window here", from the menu).
# Then, execute the command, "perl varnames.pl".

#---------------------------------------------------------------------------------------------------

# thinBasic variable names are case insensitive.
# thinBasic variable names can be any length greater than 0.
# Currently, the last character in a variable name, maybe should not be, '_' (sometimes, thinBasic becomes confused, if it is).
# The first character in a thinBasic variable name must be either, '_', 'a-z', or, 'A-Z'.
# A middle character in a thinBasic variable name must be either, '_', 'a-z', 'A-Z', or, '0-9'.
# The last character in a thinBasic variable name must be either, 'a-z', 'A-Z', or, '0-9'.

#---------------------------------------------------------------------------------------------------

# Enable the Perl, "nannies".

use warnings;
use strict;

#---------------------------------------------------------------------------------------------------

# global variables

our (%firstchars, %midchars, %lastchars);

#---------------------------------------------------------------------------------------------------

# functions

sub setsets
{
my $s = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ';
my @a = split("", $s);
%firstchars = map { $_ => 1 } @a;
$s = '0123456789';
@a = split("", $s);
%midchars = map { $_ => 1 } @a;
%midchars = (%firstchars, %midchars);
%lastchars = (%midchars);
delete($lastchars{_});
}

sub validvariablename
{
my $s = shift;
$s = uc($s);
my @a = split("", $s);
my $length = @a;
if ($length == 0){return 0;}
my $char;
my $count = -1;
foreach $char (@a)
{
$count += 1;
if ($count == 0)
{
if (not exists($firstchars{$char})) {return 0;}
}
if (($count > 0) and ($count < ($length - 1)))
{
if (not exists($midchars{$char})) {return 0;}
}
if ($count == $length - 1)
{
if (not exists($lastchars{$char})) {return 0;}
}
}
return 1;
}

#---------------------------------------------------------------------------------------------------

# program

my (@names, $name, $index);

$names[0] = '';
$names[1] = '_';
$names[2] = 'a_';
$names[3] = '6';
$names[4] = 'd';
$names[5] = '_q';
$names[6] = '_____________________________________________________6';
$names[7] = '____________________________________a___________________________________________b';
$names[8] ='gjtuhjy897058#bnguy87';
$names[9] = 'TMGIUKJ_fhrydhe_6978576_3867thgy_HTY6UY87IU';
$names[10] = 'YUHJTIGKT9687UYJHUYIRHFJGNVMBJGUT&DURHFYT76895';
$names[11] = '$';
$names[12] = 'biykh9708ou968turhfyt758395867fhvnbmgkhiukjotlgpedhtyfhcnghtyr856903euthdrut8679whdbcnvjgktoy978463tergdhfncmvjfgur86otiyughdjnvqyrhgnvmbjguy8679486849586uyjhkfmvhgnbutkfieldotiykhiukfjhmbnv78yi0tuy76urhfjhmvnbhgyturjdhfyrht78590uijkhmnljouitehdfgvnbmhjyito7980fhrudjnvmbkhiykhoedj5utjguyjhiukjmnjhnbgjhuy7uryehdgfbvhguy87958374ythfnvgjbmhkuiykdcnvhfngytu6759123dgrtdgvnghbjhmnkjiuolpdrujgkyihknmbjguhyjtoedjgnvmbjhuy48tuyjhgmbnkhiukjoedplazmcnvhgythfjguyjhitkgolrgjhmbjhuyjgutyikolpedjgmbjhmnkhiykhiu8975tuyjhu68793edjgnbmgjhmnkjiuf';

setsets;
$index = -1;

print "Valid thinBasic variable name?\n\n";

foreach $name (@names)
{
$index += 1;
if (validvariablename($name))
{
printf "%02d true\n", $index;
}
else
{
printf "%02d false\n", $index;
}
}

#---------------------------------------------------------------------------------------------------

kryton9
26-10-2010, 02:04
I never dabbled in Perl, which is amazing since it has been around for quite a while and I liked to play with lanuages. Thanks for your thoughts on how it compared to python and ruby.