Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl : Hash traversal loop problem

Options
  • 28-06-2006 1:03pm
    #1
    Registered Users Posts: 15,815 ✭✭✭✭


    I'm getting crap output from this. It seems as though

    [php]#!/usr/bin/perl -w
    use strict;
    use DB_File;
    use Fcntl;

    $DB_BTREE->{'compare'} = sub
    {
    my ($key1, $key2) = @_;
    "\L$key1" cmp "\L$key2";
    };

    # See note 1
    open(USRIDS, "userids");

    #See note 2
    my %sqlacdb;
    my $sqldbfilename = 'sqlaccounts.db';
    tie (%sqlacdb, "DB_File", $sqldbfilename, O_RDWR|O_CREAT, 0644, $DB_BTREE) || die "can't tie sqlaccounts.db";

    #See note 3
    my %usrsqldb;
    my $usrsqldbfilename = 'usrsqlmatch.db';
    tie (%usrsqldb, "DB_File", $usrsqldbfilename, O_RDWR|O_CREAT, 0644, $DB_BTREE) || die "can't tie usrsqlmatch.db";

    my $CurrUsrID;
    my $sqlkey;
    my $sqlvalue;
    my $usrkey;
    my $usrvalue;
    my $splitpass;
    my $splitflag;
    my $sqljoya;
    my $sqljoyb;

    print "start\n";


    while (<USRIDS>)
    {
    chop;
    $CurrUsrID = $_;

    print "\n" $CurrUsrID "\n";
    if (exists ($usrsqldb{$CurrUsrID}))
    {
    print $CurrUsrID "exists in" $usrsqldbfilename "\n";
    }
    else
    {
    #See Note 2
    while ( ($sqlkey, $sqlvalue) = each %usrsqldb )
    {
    ($splitpass,$splitflag) = split(',',$sqlvalue);
    print "splitpass " $splitpass "\n splitflag" $splitflag "\n";
    if ($splitflag == 'N')
    {
    print "Unassigned SQL account found\n";
    $sqljoya = $sqlkey;
    $sqljoyb = $splitpass;

    last;
    }
    else
    {
    print "+";
    }

    }
    }
    print "\n SQL Account " $sqljoya " assigned to " $CurrUsrID "\n";
    $usrsqldb{$CurrUsrID} = "$sqljoya";
    $sqlacdb{$sqljoya} = "$sqljoyb,Y";
    }

    while (($usrkey,$usrvalue) = each (%usrsqldb))
    {
    print $usrkey " uses " $usrvalue "\n";
    }

    untie %sqlacdb;
    untie %usrsqldb;
    [/php]

    Note1: Flat file list of 8char alphanumeric logins. It's the list of users that are going to be assigned SQL accounts on the system.

    Note2: hash with the SQL usernames as they keys, each corresponding value being made up of "password,N" if the account hasn't been assigned, and "password,Y" if it has already been used.

    Note3: hash with the logins as the keys, each corresponding value being set to the SQL username which it is assigned.

    I know the code is hardly clean. I'm aware there are issues with modifying hashes as their being looped through.
    I've tried to avoid that by dumping the var contents I need out of the loop into temporary vars, ending the hash traversal loop and assigning the contents of those vars to their respective hashes.

    [php]
    if ($splitflag == 'N')
    {
    print "Unassigned SQL account found\n";
    $sqljoya = $sqlkey;
    $sqljoyb = $splitpass;

    last;
    }
    [/php]
    I've an idea the problem lies around there somewhere, or the loop surrounding it.

    Is there something blatently obvious I'm missing?

    Have only been at perl for a week or three, on-and-off. I'm not a programmer, but I need this to work today.

    Any advice?


Comments

  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Good to see you using 'strict' and '-w' from the start, unfortunately this code didn't compile because of how the print statements were written. I'm going to guess that your original code is right.
    [PHP]# Current:
    print $usrkey " uses " $usrvalue "\n";
    # Should have commas between parameters
    print $usrkey, " uses ", $usrvalue, "\n";
    # or simply put everything inside one string. perl will expand the variables correctly.
    print "$usrkey uses $usrvalue\n";[/PHP]
    SyxPax wrote:
    I'm getting crap output from this.
    What is the output? What is the expected output? Any error messages?

    Can you post sample date for the 'userids' file.
    Don't forget to add comments to your copy of the code.


Advertisement