|
|
||||||
|
#1
|
|
|
|
|
I have a hash of hashes.
Some of the values that I put into hash keys have "special" characters in them (like "\" or "_"). When these values end up as keys in a hash, I get mixed results. Sometimes displaying the value of the key itself will chop off one of the special characters, but leave the rest. Also, I can display elements of the hash of hashes using: foreach $item ( keys %hashname ) But if I plug the keys in from another source (such as from an array), I cannot display the elements when those keys I plugged in contain "$". I've also seen it happen when keys I plug in contain a "_", but I can't reproduce it in the example below. So bottom line question is: How do I work with hash keys when some of the values could possibly contain special characters??? Script and output below: Perl Script: #! /usr/bin/perl use Data::Dumper; %ownerhash = ( 'Domain1\usera' => { '\\server100\Sharename1' => {fcount =>5, fsize =>100}, '\\server200\Sharename_2' => {fcount =>5, fsize =>100} }, 'Domain2\userb' => { '\\server100\Sharename1' => {fcount =>5, fsize => 100}, '\\server200\Sharename_2' => {fcount =>5, fsize => 100} }, 'Domain3\userc' => { '\\server100\Sharename1' => {fcount =>5, fsize => 100}, '\\server200\Sharename_2' => {fcount =>5, fsize => 100}, '\\server300\Sharename3$' => {fcount =>5, fsize => 100}, '\\server400\Sharename_4$' => {fcount =>5, fsize => 100} } ); my $hashcount = scalar(keys(%ownerhash)); print "\n------------------\nCount of keys in ownerhash is: $hashcount\n"; print "\n------------------\nPrint values for keys in ownerhash:\n\n"; while (($key, $value) = each %ownerhash) {print map {"\t$_\n"} "$key=>$value, "}; # print the whole thing print "\n------------------\nPrint the whole ownerhash:\n\n"; foreach $fileowner ( keys %ownerhash ) { print "\t fileowner: $fileowner\n"; foreach $sharename ( keys %{ $ownerhash{$fileowner} } ) { print "\t\t sharename: $sharename\n"; print "\t\t\t\t fcount: $ownerhash{$fileowner}{$sharename}{fcount} \n"; print "\t\t\t\t fsize: $ownerhash{$fileowner}{$sharename}{fsize} \n"; } } print "\n------------------\nPrint based on key values pulled from arrays:\n\n"; my @fileownerarray = ('Domain1\usera','Domain2\userb','Domain3\userc'); my @sharearray = ('\\server100\Sharename1','\\server200\Sharename_2 ','\\server300\Sharename3$','\\server400\Sharename _4$'); printf "%-15s%-30s%-8s%-8s\n",("owner","share","fcount","fsize"); foreach $owner (@fileownerarray) { foreach $share (@sharearray) { printf "%-15s%-30s%-8s%-8s\n",($owner,$share,$ownerhash{$owner}{$share}{fc ount},$ownerhash{$owner}{$share}{fsize}); } } Output: c:\OwnerSnap>perl hashtester.pl ------------------ Count of keys in ownerhash is: 3 ------------------ Print values for keys in ownerhash: Domain2\userb=>HASH(0x18aa740), Domain3\userc=>HASH(0x18aa650), Domain1\usera=>HASH(0x18aa998), ------------------ Print the whole ownerhash: fileowner: Domain2\userb sharename: \server200\Sharename_2 fcount: 5 fsize: 100 sharename: \server100\Sharename1 fcount: 5 fsize: 100 fileowner: Domain3\userc sharename: \server200\Sharename_2 fcount: 5 fsize: 100 sharename: \server300\Sharename3$ fcount: 5 fsize: 100 sharename: \server100\Sharename1 fcount: 5 fsize: 100 sharename: \server400\Sharename_4$ fcount: 5 fsize: 100 fileowner: Domain1\usera sharename: \server200\Sharename_2 fcount: 5 fsize: 100 sharename: \server100\Sharename1 fcount: 5 fsize: 100 ------------------ Print based on key values pulled from arrays: owner share fcount fsize Domain1\usera \server100\Sharename1 5 100 Domain1\usera \server200\Sharename_2 5 100 Domain1\usera \server300\Sharename3$ Domain1\usera \server400\Sharename_4$ Domain2\userb \server100\Sharename1 5 100 Domain2\userb \server200\Sharename_2 5 100 Domain2\userb \server300\Sharename3$ Domain2\userb \server400\Sharename_4$ Domain3\userc \server100\Sharename1 5 100 Domain3\userc \server200\Sharename_2 5 100 Domain3\userc \server300\Sharename3$ 5 100 Domain3\userc \server400\Sharename_4$ 5 100 |
|
|
|
#2
|
|
|
|
|
On Oct 19, 12:56 am, funnybr...@gmail.com wrote:
> I have a hash of hashes. This is not relevant. > Some of the values that I put into hash keys have "special" characters > in them (like "\" or "_"). When these values end up as keys in a hash, > I get mixed results. > > Sometimes displaying the value of the key itself will chop off one of > the special characters, but leave the rest. > > Also, I can display elements of the hash of hashes using: > > foreach $item ( keys %hashname ) > > But if I plug the keys in from another source (such as from an array), > I cannot display the elements when those keys I plugged in contain "$". I can see no evidence of this in your example. > I've also seen it happen when keys I plug in contain a "_", but I > can't reproduce it in the example below. > So bottom line question is: How do I work with hash keys when some of > the values could possibly contain special characters??? No special action is required. Your problem is that you are mistaking the behaviour of backslashes within single quotes in Perl. Try my $foo = '\\'; print "[[[$foo]]]\n"; Then read up on "quote and quote like operators" in Perl. |
|
|
| Similar Threads | |
| Field Names: "LongName", "ShortName", "Code", "Description","Comments" Is anybody plugged into some sort of standard that defines what various common field names indicate. e.g. "ShortName" = Text field 10 chars or less, unique,... |
|
| Does Python have equivalent to MATLAB "varargin", "varargout", "nargin", "nargout"? Thank you in advance for your response. Dmitrey |
|
| <input id="iPhoto" type="file" size="20" runat="server"> Is there a way to restrict the user to only selecting and sending either a ..gif or .jpg. Everything I have read says this option can not be done by design (security... |
|
| Manual "Windows Update" produces "ActiveX/active scripting" error message even with "LOW" security level setting in "Trusted" Zone As of a few days ago, my efforts to update Windows XP Home using IE 6.0 produce a message that instructs me to list 2 URL's in the Trusted Zone and make sure ActiveX Controls... |
|
| Kotor Reviews: "Flawless" "Game of the Year Nomination" "Required Playing" "Better than any other Star Wars property in years" "The best RPG on the Xbox and arguably the best Star Wars game to date... KOTOR is required playing for Xbox owners, and it’s worth finishing twice to spot the differences... |
|
|
All times are GMT. The time now is 05:55 AM. | Privacy Policy
|