Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Aliases in Perl

Posted on 2005-03-21
9
Medium Priority
?
192 Views
Last Modified: 2010-03-05
Is it possible to have use aliases for the names in the line beginning with "dll"?  The names are mapped to the uidks in %hash.  I don't want to go in and substitue all the names with uidks so can I just use an alias somehow?  Or to the names have to be varibles?

%hash = (
   admin => uidk5060,
   frank => uidk5008,
   harry => uidk5276,
   joe => uidk5762,
   tom => uidk5189,
   ...
);
...
   "dll" => "admin;tom;frank;harry;joe;",
...


The only other option I could think of was to move the names out of the hash so it looks like this:

$admin = "uidk5060";
$frank = "uidk5008";
...

and change all names in "dll" to:

   "dll" => "$admin;$tom;$frank;$harry;$joe;",

0
Comment
Question by:mock5c
8 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 13596130
You could use a master hash (like %hash in your case) and an indirect hash, containing an array of master hash keys for each key.

Example:

%hash = (
   admin => uidk5060,
   frank => uidk5008,
   harry => uidk5276,
   joe => uidk5762,
   tom => uidk5189,
   ...
);

%indirect_hash = (
  admin => ['admin'],
  frank => ['frank'],
  ....
  dll => ['admin', 'tom', 'frank', 'harry', 'joe']
);

Does this help you?




0
 
LVL 18

Expert Comment

by:kandura
ID: 13596323
Stefans suggestion to use array references is likely a good one, but it is difficult for us to suggest a good datastructure without knowing how you're using them in the rest of your code.
Can you tell us a bit more about what you do with the master hash, and with that 'dll' key?
0
 
LVL 85

Expert Comment

by:ozo
ID: 13596334
What do you want to do with the hash that you think requires an alias?
Do you want something like this?

"dll" => (join';',@hash{qw(admin tom frank harry joe)},''),
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:stefan73
ID: 13596725
Hmm, this gets ugly when one of the users changes his UID.

But yes, something like
$hash{dll} = join('',map {$_.';'} values %hash);

can help.
0
 
LVL 85

Expert Comment

by:ozo
ID: 13596800
As kandura suggests, it might help to know in what context
   "dll" => "admin;tom;frank;harry;joe;",
or
   "dll" => "$admin;$tom;$frank;$harry;$joe;",
is meant to be used, and what relationship this should have to %hash
0
 

Author Comment

by:mock5c
ID: 13604792
my %no_ci_typelist = (
    "dll" => "admin;burns;reynolds;johnson;", # remember the ; after the last name
    "lib" => "admin;burns;reynolds;johnson;",
    "tlb" => "admin;burns;reynolds;johnson;funk;holt;",
    "_ZZZZ_" => "_ZZZZ_"  # DO NOT DELETE THIS LINE
);
...
if ($user_authorized == 1 && exists($no_ci_typelist{"$element_ext"})) {
    $restriction_type = "type";
    $authorized_list = $no_ci_typelist{"$element_ext"};
    if (index("$authorized_list", "${username};") < 0) {
        $user_authorized = 0;
    }
}
...

It's used in the above context.  All usernames have changed to uidkxxxx (where x = 0-9).  So, I can go through many scripts and do a replace (or write a perl script to do that for me).  Or I could set up variables at the beginning such that:

my $admin = "uidk6316";
my $burns = "uidk5199";
...

And just turn the names into variables so lines in %no_ci_typelist
would change to something like:
   "dll" => "$admin;$burns;$reynolds;$johnson;", # remember the ; after the last name
   "lib" => "$admin;$burns;$reynolds;$johnson;",
   etc.

I figured I'd learn something about using aliases if I did it the second way.  I'm trying to avoid making too many modifications where the names appear.






0
 
LVL 85

Expert Comment

by:ozo
ID: 13605630
#If all the uidk's are unique, you might do something like:
my %hash = (
   admin => uidk5060,
   frank => uidk5008,
   harry => uidk5276,
   joe => uidk5762,
   tom => uidk5189,
   ...
);
my %uidk = reverse %hash;
my %no_ci_typelist = (
    "dll" => "admin;burns;reynolds;johnson;", # remember the ; after the last name
    "lib" => "admin;burns;reynolds;johnson;",
    "tlb" => "admin;burns;reynolds;johnson;funk;holt;",
    "_ZZZZ_" => "_ZZZZ_"  # DO NOT DELETE THIS LINE
);
...
if ($user_authorized == 1 && exists($no_ci_typelist{$element_ext})) {
    $restriction_type = "type";
    $authorized_list = $no_ci_typelist{$element_ext};
    if( index($authorized_list, "$uidk{$username};" ) < 0 ){
        $user_authorized = 0;
    }
}
0
 
LVL 85

Accepted Solution

by:
ozo earned 225 total points
ID: 13606534
#you could also build
my %hash = (
   admin => uidk5060,
   frank => uidk5008,
   harry => uidk5276,
   joe => uidk5762,
   tom => uidk5189,
   ...
);
my %no_ci_typelist = (
    "dll" => "admin;burns;reynolds;johnson;", # remember the ; after the last name
    "lib" => "admin;burns;reynolds;johnson;",
    "tlb" => "admin;burns;reynolds;johnson;funk;holt;",
    "_ZZZZ_" => "_ZZZZ_"  # DO NOT DELETE THIS LINE
);
my %authorized = %no_ci_typelist;
$_ = {map{$hash{$_}=>1}split/;/,$_} for values %authorized;
#then test
if ($user_authorized == 1 && exists($no_ci_typelist{$element_ext})) {
    $restriction_type = "type";
    if( !$authorized{$element_ext}{$username} ){
        $user_authorized = 0;
    }
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question