Results 1 to 2 of 2

Thread: Perl Sets

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152

    Perl Sets

    [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...cseen#msg27313

    Ruby --> http://community.thinbasic.com/index...cseen#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).


    Dan

    [code=perl]#---------------------------------------------------------------------------------------------------
    # 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;
    }
    }

    #---------------------------------------------------------------------------------------------------
    [/code]
    Attached Files Attached Files
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

Similar Threads

  1. Lua Sets
    By danbaron in forum Scripting
    Replies: 0
    Last Post: 12-11-2010, 07:10
  2. Python Sets
    By danbaron in forum Scripting
    Replies: 5
    Last Post: 24-10-2010, 06:10
  3. Ruby Sets
    By danbaron in forum Scripting
    Replies: 0
    Last Post: 15-10-2010, 20:44
  4. Infinite Sets
    By danbaron in forum Math: all about
    Replies: 7
    Last Post: 28-01-2010, 10:01

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •