Home > Archive > PERL Beginners > December 2006 > reg exp continued need pulled from reference
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
reg exp continued need pulled from reference
|
|
| Oryann9 2006-12-18, 9:59 pm |
| Hello...
I have thought about this one and tried various code changes but cannot get what I want.
My problem: mismatched UIDs in password files.
My solution:
#1 store passwd files in a globbed array
#2 create array reference from globbed array
#3 open array ref, create hash with more than one value per key. Keys are regexps from filenames
#4 read in every line of passwd files from reference. Values in hash need to be passwd entries
For example: key => servernames.platform (dubhpr01.hpux)
values => filelds from passwd file (name,uid,gid,comments)
#5 Once hash is built, traverse through searching for usernames that have unlike UIDs from all files, then print these to an xls using SpreadSheet::WriteExcel.
In my code I have completed 1,2 and 3. Started 4 but I am printing the array reference address as opposed to printing the actual values. What am I doing wrong and any tips would be nice?
_OUTPUT_
KEY dubhst14.hpux
ELEMENTS /home/dbsmith/passwd.dubhst14.hpux
DUB VALUES root
KEY dubhdv05.hpux
ELEMENTS /home/dbsmith/passwd.dubhdv05.hpux
DUB VALUES root
dubhst14.hpux => ARRAY(0x4002b0f8)
dubhdv05.hpux => ARRAY(0x4059c9a4)
dubhadm3.hpux => ARRAY(0x4059c8f0)
dwhetls2.hpux => ARRAY(0x4002c164)
dubhadm1.hpux => ARRAY(0x4059c8a8)
oftappp1.hpux => ARRAY(0x405a2084)
dubhpr28.hpux => ARRAY(0x4002e1bc)
cic2.hpux => ARRAY(0x4059c6f8)
#!/usr/bin/perl
##-- Initialize environment --##
use strict;
use warnings;
use diagnostics;
use Spreadsheet::WriteExcel;
#use Data::Dumper;
$ENV{"PATH"} = qq(/usr/bin:/bin:/home/dbsmith:/home/dbsmith/McGaw);
delete @ENV{qw (IFS CDPATH ENV KSH_ENV BASH_ENV)};
##-- Central DIE routine --##
open (LOG, ">>/tmp/uid_ck.log") or warn "uid_ck.log did not open $!";
my $overide = $SIG{__DIE__}; ## get error handler currently assigned 2 die
$SIG{__DIE__} = sub {
my $error = shift; ## error now holds the mesg passed to die
$overide->($error) if ( ref $overide );
print LOG ($error);
};
##-- BEGIN MAIN --##
my @dublinaray = glob("/home/dbsmith/passwd.*");
my $dublin_aref = \@dublinaray;
my @mcgawaray = glob("/home/dbsmith/McGaw/passwd.*");
my $mcgaw_aref = \@mcgawaray;
my (%dublin_hosts,%mcgaw_hosts) = ();
my ($dub_key,$dub_values,$mcg_key,$mcg_valu
es);
parse_file();
sub parse_file {
foreach my $element ( @{$dublin_aref} ) {
{ local *FILE;
open (FILE, "+<$element") or die "dublin reference did not open: $!";
local $/ = undef;
($dub_key) = $element =~ m|\.(\w+\.\w+)\z|i;
($dub_values) = split /:/, <FILE> ;
push ( @{$dublin_hosts{$dub_key}}, $dub_values );
print "KEY\t",$dub_key,"\n\n";
print "ELEMENTS\t",$element,"\n\n";
print "DUB VALUES\t",$dub_values,"\n\n";
}
}
while ( ($dub_key,$dub_values) = each %dublin_hosts ) {
print "$dub_key => $dub_values\n";
}
#foreach my $host (sort keys %dublin_hosts) {
# print "$host: @{$dublin_hosts{$dub_key}}\n";
#}
#foreach my $element2 ( @{$mcgaw_aref} ) {
# { local *FILE2;
# open (FILE2, "+<$element2") or die "mcgaw reference did not open: $!";
# local $/ = undef;
# ($mcg_key) = $element2 =~ m|\.(\w+\.\w+)\z|i;
# push (@{$mcgaw_hosts{$mcg_key}}, $mcg_values );
# print "\n$element2\n\n";
# print ;
# }
#}
} ##-- END SUB --##
________________________________________
__________
I looked in the cookbook and as I tried to implement is below. Am I on the right road?
thank you
5.7. Hashes with Multiple Values Per Key Problem You want to store more than one value for each key.
Solution Store an array reference in $hash{$key}, and put the values into that array.
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
| |
| D. Bolliger 2006-12-18, 9:59 pm |
| oryann9 am Montag, 18. Dezember 2006 16:55:
> Hello...
> I have thought about this one and tried various code changes but cannot
> get what I want.
>
> My problem: mismatched UIDs in password files.
> My solution:
>
> #1 store passwd files in a globbed array
> #2 create array reference from globbed array
> #3 open array ref, create hash with more than one value per key. Keys are
> regexps from filenames #4 read in every line of passwd files from
> reference. Values in hash need to be passwd entries For example: key =>
> servernames.platform (dubhpr01.hpux)
> values => filelds from passwd file (name,uid,gid,comments)
> #5 Once hash is built, traverse through searching for usernames that have
> unlike UIDs from all files, then print these to an xls using
> SpreadSheet::WriteExcel.
>
> In my code I have completed 1,2 and 3. Started 4 but I am printing the
> array reference address as opposed to printing the actual values. What am I
> doing wrong and any tips would be nice?
>
> _OUTPUT_
>
> KEY dubhst14.hpux
> ELEMENTS /home/dbsmith/passwd.dubhst14.hpux
> DUB VALUES root
>
> KEY dubhdv05.hpux
> ELEMENTS /home/dbsmith/passwd.dubhdv05.hpux
> DUB VALUES root
>
>
> dubhst14.hpux => ARRAY(0x4002b0f8)
> dubhdv05.hpux => ARRAY(0x4059c9a4)
> dubhadm3.hpux => ARRAY(0x4059c8f0)
[snipped]
> #!/usr/bin/perl
>
> ##-- Initialize environment --##
> use strict;
> use warnings;
> use diagnostics;
> use Spreadsheet::WriteExcel;
[snipped]
> print "$dub_key => $dub_values\n";
[snipped]
Derek,
I guess the reason why you got no answer when you posted the identical
question in a recent thread is because, at least
- your question(s) is/are unclear
- your code is not trimmed - even not from comments
Anyway. When you print $dub_values and it shows up as an array ref as
ARRAY(0x4002c164)
and you want the values of this arrayref, you have to dereference it in some
way.
Start with printing out @$dub_values.
Or assign it before if you want bigger freedom to format the values:
my @dub_values_arr=@$dub_values;
# handle @dub_values_arr
Hope this helps
Dani
| |
| Oryann9 2006-12-18, 9:59 pm |
|
"D. Bolliger" <info@dbolliger.ch> wrote:
I guess the reason why you got no answer when you posted the identical
question in a recent thread is because, at least
- your question(s) is/are unclear
- your code is not trimmed - even not from comments
Anyway. When you print $dub_values and it shows up as an array ref as
ARRAY(0x4002c164)
and you want the values of this arrayref, you have to dereference it in some
way.
Start with printing out @$dub_values.
Or assign it before if you want bigger freedom to format the values:
my @dub_values_arr=@$dub_values;
# handle @dub_values_arr
Hope this helps
Dani
How are my quesitons unclear??? Man I take a beating from this help list. I have seen many questions asked that are just "bone-headed" questions or people are too lazy to research and I do not feel my questions fall in these categories.
I stated " I am printing the array reference address as opposed to printing the actual values.
What am I doing wrong and any tips would be nice?"
How is that unclear?
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
| |
| D. Bolliger 2006-12-18, 9:59 pm |
| oryann9 am Montag, 18. Dezember 2006 19:52:
> "D. Bolliger" <info@dbolliger.ch> wrote:
[snipped]
> How are my quesitons unclear???
[snipped]
I answered offlist. Sorry to all for the noise of this notice.
Dani
| |
| Oryann9 2006-12-19, 7:01 pm |
| "D. Bolliger" <info@dbolliger.ch> wrote: oryann9 am Montag, 18. Dezember 2006 19:52:
> "D. Bolliger" wrote:
[snipped]
> How are my quesitons unclear???
[snipped]
I answered offlist. Sorry to all for the noise of this notice.
Dani
****************************************
****************************
thank god!
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
| |
| Oryann9 2006-12-19, 7:01 pm |
|
Anyway. When you print $dub_values and it shows up as an array ref as
ARRAY(0x4002c164)
and you want the values of this arrayref, you have to dereference it in some
way.
Start with printing out @$dub_values.
Or assign it before if you want bigger freedom to format the values:
my @dub_values_arr=@$dub_values;
# handle @dub_values_arr
Hope this helps
Dani
I don't mean to sound negative, but did you even look at my code? I know how to dereference and I was just printing this in my debugging efforts.
dublin_aref = \@dublin_array.
Again, @dublin_arry contains a glob like so: glob("/home/oryann9/passwd.*)
dub_values are the values within the hash %dublin_hosts while dub_key is the regexp from the filenames that is the key within the hash %dublin_hosts.
As an example, each key is the regexp of each file name from the array reference: passwd.dubhpr01.hpux after being parsed by my regexp I get dubhpr01.hpux
Each values are the various fields in the passwd files.
My Goal is below
key=>dubhpr01.hpux
values=>name,uid,gid,comments
Here is the snippet of code I am working on now:
1 use strict;
2 use warning;
3 use diagnostics;
4
5 my @dublinaray = glob("/home/oryann9/passwd.*");
6 my $dublin_aref = \@dublinaray;
7 my (%dublin_hosts) = ();
8 my ($dub_key,$dub_values,);
9
10 parse_file();
11
12
13 sub parse_file {
14 foreach my $element ( @{$dublin_aref} ) {
15 { local *FILE;
16 open (FILE, "+<$element") or die "dublin reference did not open: $!";
17 local $/ = undef;
18 ($dub_key) = $element =~ m|\.(\w+\.\w+)\z|i;
19 ($dub_values) = split /:/, <FILE>;
20 push ( @{$dublin_hosts{$dub_key}}, $dub_values );
21 print Dumper("KEY\t",$dub_key,"\n\n");
22 print Dumper("ELEMENTS\t",$element,"\n\n");
23 print Dumper("DUB VALUES\t",$dub_values,"\n\n");
24 }
25 }
26 while ( ($dub_key,$dub_values) = each %dublin_hosts ) {
27 print Dumper("$dub_key => $dub_values\n");
28 }
29
30 } ##-- END SUB --##
Here is my question
I have a line of code like so: "$dub_key => $dub_values\n";
that prints
dubhpr28.hpux => ARRAY(0x4002e1bc)
but I want to have printed out the values contained in the $dub_values
arrayref instead of its address.
What do I have to change to achieve this?
thank you
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
| |
| Oryann9 2006-12-19, 7:01 pm |
|
oryann9 <oryann9@yahoo.com> wrote:
Here is my question
I have a line of code like so: "$dub_key => $dub_values\n";
that prints
dubhpr28.hpux => ARRAY(0x4002e1bc)
but I want to have printed out the values contained in the $dub_values
arrayref instead of its address.
What do I have to change to achieve this?
thank you
I got it working...never mind.
I shall be back... : )
thank you
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
|
|
|
|
|