#!/usr/bin/perl -w
use strict;
my $userlist = &BuildUserList();
sub BuildUserList {
open (EP,"</etc/passwd") || die "Can't fork open EP: $!\n";
my %users = ();
while (<EP>) {
chomp;
my ($user,$pass,$uid,$gid,$gecos,$home,$shell) = (split':');
$users{$user} = join(':',$pass,$uid,$gid,$gecos,$home,$shell);
}
close (EP) || die "Can't fork close EP: $!\n";
return \%users;
}
Effectively, I'd like to combine the split and join statements above,
but I'm not sure exactly how to do that, or if it's even a viable
solution.
TIA,
Drew Myers
> I'm writing a small perl program to parse the contents of a Unix
> /etc/passwd file into a hash. I'm using a split to separate the
> fields in /etc/passwd, but I actually just want to split the first
> one (the login name), and then dump the remaining fields into a
> scalar to form the value portion of the hash.
[snip]
> my ($user,$pass,$uid,$gid,$gecos,$home,$shell) = (split':');
> $users{$user} = join(':',$pass,$uid,$gid,$gecos,$home,$shell);
[snip]
> Effectively, I'd like to combine the split and join statements
> above, but I'm not sure exactly how to do that, or if it's even a
> viable solution.
split takes an optional third argument that limits the number of
fields.
my ($user, $rest) = split (/:/, $_, 2);
$users{$user} = $rest;
--
EBC
(snipped)
> I'm writing a small perl program to parse the contents of a Unix
> /etc/passwd file into a hash. I'm using a split to separate the
> fields in /etc/passwd, but I actually just want to split the first one
> (the login name), and then dump the remaining fields into a scalar to
> form the value portion of the hash.
> while (<EP>) {
> chomp;
> my ($user,$pass,$uid,$gid,$gecos,$home,$shell) = (split':');
> $users{$user} = join(':',$pass,$uid,$gid,$gecos,$home,$shell);
> }
> I'd like to combine the split and join statements above
You will discover my test script below my signature to
be relatively quick and efficient for your task. However,
a simple associative array would be more efficient. A
hash should be a second choice to an array method for
most programming circumstances. What you do later in
your program would be a deciding factor on which,
an array or a hash, would be best to use.
Preparative logic should be foremost in your mind when
programming. I often, almost always, see a failure of
regulars here to use, less understand, preparative logic.
This is the nature of Language Lawyers rather than the
nature of real programmers.
Godzilla!
--
TEST SCRIPT:
____________
#!perl
while (<DATA>)
{ $Users{substr ($_, 0, index ($_, ":"))} = substr ($_, index ($_, ":") + 1); }
print "@{[ %Users ]}";
__DATA__
user1:pass1:uid1:gid1:gecos1:home1:shell1
user2:pass2:uid2:gid2:gecos2:home2:shell2
PRINTED RESULTS:
________________
user1 pass1:uid1:gid1:gecos1:home1:shell1
user2 pass2:uid2:gid2:gecos2:home2:shell2
EC> split takes an optional third argument that limits the number of
EC> fields.
EC> my ($user, $rest) = split (/:/, $_, 2);
EC> $users{$user} = $rest;
even simpler:
%users = map split (/:/, $_, 2), <PW> ;
that will leave a newline on the values but that can be removed with:
chomp values %users ;
<only recent perl's can do lvalue values>
uri
--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Godzilla> You will discover my test script below my signature to
Godzilla> be relatively quick and efficient for your
Godzilla> task. However, a simple associative array would be more
Godzilla> efficient. A hash should be a second choice to an array
Godzilla> method for most programming circumstances. What you do
Godzilla> later in your program would be a deciding factor on
Godzilla> which, an array or a hash, would be best to use.
So what's the difference between a hash and and associative array?
--
Dale Henderson
"Imaginary universes are so much more beautiful than this stupidly-
constructed 'real' one..." -- G. H. Hardy
You can always use the regex approach
if(/^([^:]+);(.*)/){
$users{$1}=$2;
}else{
die "Bad file format"
}
Even has th advantage of checking your data file.
Format, creation and access methodologies along with efficiency.
An array is most often superior for all criteria.
My suggestion is you research and read about arrays and hashes.
It is clear you have a bit of Perl learning yet to accomplish;
differences between arrays and hashes, are readily apparent.
Godzilla!
> chomp values %users ;
>
><only recent perl's can do lvalue values>
For older perls, you can use a hash slice:
chomp @users{keys %users};
--
Bart.
>> So what's the difference between a hash and and associative array?
>
>Format, creation and access methodologies along with efficiency.
You stupid person. A "hash" is simply another shorter name for an
"associative array". Technically, they're the same thing.
So what's the difference between a car and an automobile?
--
Bart.
>>>>> "DM" == Drew Myers <bh_...@hotmail.com> writes:
DM> Effectively, I'd like to combine the split and join statements
DM> above, but I'm not sure exactly how to do that, or if it's
DM> even a viable solution.
DH> You can always use the regex approach
DH> if(/^([^:]+);(.*)/){
^
tr/;/:/
and make sure you read bart's reply to moronzilla. you will learn why
she should be ignored here. first she says use associative arrays over
hashes which is plain stupid. then she says use arrays over hashes which
for this problem (indexing by user names from /etc/passwd) is plain
wrong. so which is she, stupid or wrong?
You state, with clarity, an associative array and a hash
are the same, identical, no difference between the two.
This you have stated, without any doubt.
Based on your high logic, my script beneath my signature will
perform with perfection by printing my data, flawlessly.
Godzilla!
--
#!perl
while (<DATA>)
{
$Users{substr ($_, 0, index ($_, ":"))} = substr ($_, index ($_, ":") + 1);
push (@Array, $_);
}
print "Hash:\n %Users\n\n";
print "Array:\n @Array";
(snipped)
> and make sure you read bart's reply to moronzilla. you will learn why
> she should be ignored here. first she says use associative arrays over
> hashes which is plain stupid. then she says use arrays over hashes which
> for this problem (indexing by user names from /etc/passwd) is plain
> wrong. so which is she, stupid or wrong?
pfffttt... you are pissed off because you cannot
create a methodolgy which is quicker and more
efficient than mine.
Such a delicate easily frightened little boy.
Will you forgive me for being a more talented
Perl programmer than you? I just cannot help it.
Truly, it is not my fault nor intentional.
Godzilla!
You fail to see the difference between the technical terms "array" and
"associative array". Associative array is the old name for a hash.
This terminology was in use in the Perl 4 documentation, but is no
longer used.
Repeat: An associative array is not an array. It was a misnomer. The
name was changed to hash, precisely because the thing was never an
array in the first place.
No one claimed an array was the same thing as a hash.
Martien
--
|
Martien Verbruggen | Unix is user friendly. It's just
Trading Post Australia Pty Ltd | selective about its friends.
|
> Godzilla! wrote:
> > Bart Lateur wrote:
> > > Godzilla! wrote:
> No one claimed an array was the same thing as a hash.
Bart did.
You have forgotten you have me killfiled.
Do try to remember this. Looks bad when
you and others whom have killfiled me,
respond to my posts. Lately, a lot of
you boys are forgetting you have me
killfiled. Very odd indeed!
Beneath my signature, you will find an associative array
and a hash. Based on your statements and high logic, which
is no different than Bart's nor Uri's, hmm.. are you boys
lovers are just the same person? Anyhow, based on your
flawless logic, my script will exhibit identical prints,
with typical Godzilla perfection.
Godzilla!
--
#!perl
@Associative_Array = qw (user1:pass1:uid1:gid1:gecos1:home1:shell1
user2:pass2:uid2:gid2:gecos2:home2:shell2);
$Hash{user1} = "pass1:uid1:gid1:gecos1:home1:shell1";
$Hash{user2} = "pass2:uid2:gid2:gecos2:home2:shell2";
for (@Associative_Array)
{ print "Array: $_\n"; }
print "\n\n";
for (%Hash)
{ print "Hash: $_\n"; }
exit;
No. He did not. He said an _associative_ array was a hash. That is not
the same thing.
> You have forgotten you have me killfiled.
I haven't. I changed the scoring in my score file. You can rest
assured that I am regretting that mistake, and will rectify that.
> Beneath my signature, you will find an associative array
You still don't get it do you?
> @Associative_Array = qw (user1:pass1:uid1:gid1:gecos1:home1:shell1
> user2:pass2:uid2:gid2:gecos2:home2:shell2);
This is NOT an associative array. It is an array. Learn the
difference.
And now you go back in your hole, you troll.
Martien
--
|
Martien Verbruggen | 42.6% of statistics is made up on the
Trading Post Australia Pty Ltd | spot.
|
> Godzilla! wrote:
> > Martien Verbruggen wrote:
> >> Godzilla! wrote:
> >> > Bart Lateur wrote:
> >> > > Godzilla! wrote:
(Don't forget Uri! He is in on this one!)
> > You have forgotten you have me killfiled.
> I haven't. I changed the scoring in my score file. You can rest
> assured that I am regretting that mistake, and will rectify that.
Must be a contagion. Like I said, a lot of you boys have
forgotten you have me killfiled. I still think this is
quite odd; so many forgetting all at the same time. Kinda
like you are all affected by the same forgetfulness. This
is surely more than mere coincidence.
> > Beneath my signature, you will find an associative array
> You still don't get it do you?
Oh, rest assured I get it. However you don't but you
are about to understand.
You really have a thing for this word "get" just like,
actually identically like, a few others here.
> > @Associative_Array = qw (user1:pass1:uid1:gid1:gecos1:home1:shell1
> > user2:pass2:uid2:gid2:gecos2:home2:shell2);
> This is NOT an associative array. It is an array. Learn the
> difference.
> And now you go back in your hole, you troll.
You give up? Appears so. Frustrating, ain't I?
An associative array today is called a hash. For a decade
or more, it is said, "Call it a hash!" So, this expression
"associative array" is free for use. Virtually none, actually
none in this group use the expression associative array
except for me. I view an array containing elements with
associated values to be, of all things, an associative
array. Clever huh? However, playing a game of semantic
slipperies does not interest me. As a well recognized
Semantic Guerrilla, you boys present no challenge.
What does interest me is this. All three of you elected
to respond to the same article of mine all within the
same temporal period. All three of you reacted with
identical thinking, identical logic along with identical
intensity, identical vehemence and many other identical
personality quirks; personal idioms, such as psychotically
defending each other. It is as if, it is a certainty,
all three of you share the same brain, the same thinking,
the same logic and, the same personality.
Clever, ain't I? Want to continue playing mind games?
Around here, I am certainly the best at mind games,
wouldn't you agree, Frank?
Godzilla! Queen Of Semantic Guerrillas.
>>>>>> "EC" == E Chang <ech...@netstorm.net> writes:
> EC> my ($user, $rest) = split (/:/, $_, 2);
> EC> $users{$user} = $rest;
>
> even simpler:
>
> %users = map split (/:/, $_, 2), <PW> ;
Sweet. I was hoping someone would point out a compact improvement like
that.
--
EBC
G> Uri Guttman wrote:
>> Dale Henderson wrote:
>> > Drew Myers wrote:
G> (snipped)
>> and make sure you read bart's reply to moronzilla. you will learn why
>> she should be ignored here. first she says use associative arrays over
>> hashes which is plain stupid. then she says use arrays over hashes which
>> for this problem (indexing by user names from /etc/passwd) is plain
>> wrong. so which is she, stupid or wrong?
G> Will you forgive me for being a more talented
G> Perl programmer than you? I just cannot help it.
G> Truly, it is not my fault nor intentional.
i forgive you for your stupidity. we are all stupid sometimes. but i
don't forgive you for your constant thickheadedness. you never seem to
grasp that hashes ARE associative arrays by another name and they are
NOT arrays. but then you won't look back on your recent posts and
realize that mistake. but it is good that you make such dumb mistakes in
public for even newbies will know that you don't know anything about
perl. as for your being a better programmer, i let you win that
battle. i will not fight the lame, the sick, or the mentally
handicapped.
G> Martien Verbruggen wrote:
>> Godzilla! wrote:
>> > Bart Lateur wrote:
>> > > Godzilla! wrote:
>> No one claimed an array was the same thing as a hash.
G> Bart did.
bart said:
You stupid person. A "hash" is simply another shorter name for an
"associative array". Technically, they're the same thing.
he was correct in all three statements.
G> Beneath my signature, you will find an associative array
G> and a hash. Based on your statements and high logic, which
G> is no different than Bart's nor Uri's, hmm.. are you boys
G> lovers are just the same person? Anyhow, based on your
G> flawless logic, my script will exhibit identical prints,
G> with typical Godzilla perfection.
G> @Associative_Array = qw (user1:pass1:uid1:gid1:gecos1:home1:shell1
G> user2:pass2:uid2:gid2:gecos2:home2:shell2);
that is an ARRAY. not an ASSOCIATIVE ARRAY. note the adjective
ASSOCIATIVE which modifies the word ARRAY. perl4 had ASSOCIATIVE
ARRAYs. perl5 renamed them HASHES
G> $Hash{user1} = "pass1:uid1:gid1:gecos1:home1:shell1";
G> $Hash{user2} = "pass2:uid2:gid2:gecos2:home2:shell2";
G> for (@Associative_Array)
G> { print "Array: $_\n"; }
G> print "\n\n";
G> for (%Hash)
G> { print "Hash: $_\n"; }
notice how the ARRAY doesn't print any of the user keys? BECAUSE IT
ISN'T ASSOCIATIVE. it doesn't ASSOCIATE keys with values.
i hope all you newbies read this and learn how dumb she is.
> i forgive you for your stupidity. we are all stupid sometimes. but i
> don't forgive you for your constant thickheadedness. you never seem to
> grasp that hashes ARE associative arrays by another name and they are
> NOT arrays. but then you won't look back on your recent posts and
> realize that mistake. but it is good that you make such dumb mistakes in
> public for even newbies will know that you don't know anything about
> perl. as for your being a better programmer, i let you win that
> battle. i will not fight the lame, the sick, or the mentally
> handicapped.
Oh shoot Uri, this is no battle. My being a better
Perl programmer than you, is just the way it is.
You know Uri, I wouldn't pull stunts like this if
you and the other boys here didn't constantly and
continuously harass me, insult me and troll me.
You boys always start this. You boys are stalking
me through four different Perl groups.
You boys are not having much fun.
I am having a lot of fun.
Do you want to have fun?
Just a matter of knocking off all the childish
crap, don't you think?
Do so and I may be motivated to stop munching your minds.
Godzilla!
> Uri Guttman <u...@stemsystems.com> wrote in
> news:x7bshxd...@home.sysarch.com:
>
> > %users = map split (/:/, $_, 2), <PW> ;
>
> Sweet. I was hoping someone would point out a compact improvement like
> that.
Of course, that assumes the size of your /etc/passwd is not an
appreciable fraction of the memory you have :)
Cheers,
Philip
--
Philip Newton <nospam...@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
Well, regardless of the "battle" that is or is not being waged, here
are the benchmark results I come up with:
Benchmark: timing 100 iterations of map, split, substr...
map: 1 wallclock secs ( 0.21 usr + 0.01 sys = 0.22 CPU) @
454.55/s (n=100)
(warning: too few iterations for a reliable count)
split: 0 wallclock secs ( 0.30 usr + 0.01 sys = 0.31 CPU) @
322.58/s (n=100)
(warning: too few iterations for a reliable count)
substr: 0 wallclock secs ( 0.22 usr + 0.01 sys = 0.23 CPU) @
434.78/s (n=100)
(warning: too few iterations for a reliable count)
Benchmark: timing 1000 iterations of map, split, substr...
map: 2 wallclock secs ( 2.14 usr + 0.09 sys = 2.23 CPU) @
448.43/s (n=1000)
split: 4 wallclock secs ( 3.05 usr + 0.09 sys = 3.14 CPU) @
318.47/s (n=1000)
substr: 2 wallclock secs ( 2.19 usr + 0.08 sys = 2.27 CPU) @
440.53/s (n=1000)
Benchmark: timing 10000 iterations of map, split, substr...
map: 22 wallclock secs (21.35 usr + 0.84 sys = 22.19 CPU) @
450.65/s (n=10000)
split: 32 wallclock secs (30.29 usr + 0.85 sys = 31.14 CPU) @
321.13/s (n=10000)
substr: 23 wallclock secs (21.83 usr + 0.86 sys = 22.69 CPU) @
440.72/s (n=10000)
So, map wins in all 3 instances I tested (100,1000,10000 iterations).
Next is Godzilla's substr, and finally, what I think of as a
"traditional" split.
Thanks to everyone for your help!
I've learned a lot,
Drew
>Dale Henderson wrote:
>
>> > Godzilla wrote:
snip
>
>> So what's the difference between a hash and and associative array?
>
>
>Format, creation and access methodologies along with efficiency.
>
>An array is most often superior for all criteria.
>
>My suggestion is you research and read about arrays and hashes.
>It is clear you have a bit of Perl learning yet to accomplish;
>differences between arrays and hashes, are readily apparent.
Even _I_ know that a hash and an associative array are terms for the
same thing!
Pick better fights you'll come off looking smarter. This is like a
newbie troll. You're better than that.
Here are some benchmark results which have not been
falsely fabricated. As you know, map methods become
less efficient with larger data samples, Frank.
Godzilla!
--
#!perl
print "Content-type: text/plain\n\n";
use Benchmark;
print "Run One:\n\n";
&Time;
print "\n\nRun Two:\n\n";
&Time;
print "\n\nRun Three:\n\n";
&Time;
sub Time
{
timethese (1000000,
{
'name1' =>
'%users = map split (/:/, $_, 2), <DATA> ;',
'name2' =>
'while (<DATA>)
{ $Users{substr ($_, 0, index ($_, ":"))} = substr ($_, index ($_, ":") + 1); }',
} );
}
__DATA__
user1:pass1:uid1:gid1:gecos1:home1:shell1
user2:pass2:uid2:gid2:gecos2:home2:shell2
PRINTED RESULTS:
________________
Run One:
Benchmark: timing 1000000 iterations of name1, name2...
name1: 26 wallclock secs (27.51 usr + 0.00 sys = 27.51 CPU) @ 36350.42/s
name2: 26 wallclock secs (25.82 usr + 0.00 sys = 25.82 CPU) @ 38729.67/s
Run Two:
Benchmark: timing 1000000 iterations of name1, name2...
name1: 28 wallclock secs (27.58 usr + 0.00 sys = 27.58 CPU) @ 36258.16/s
name2: 26 wallclock secs (26.04 usr + 0.00 sys = 26.04 CPU) @ 38402.46/s
Run Three:
Benchmark: timing 1000000 iterations of name1, name2...
name1: 28 wallclock secs (27.79 usr + 0.00 sys = 27.79 CPU) @ 35984.17/s
name2: 26 wallclock secs (25.64 usr + 0.00 sys = 25.64 CPU) @ 39001.56/s
>Here are some benchmark results which have not been
>falsely fabricated. As you know, map methods become
>less efficient with larger data samples, Frank.
>sub Time
> {
> timethese (1000000,
> {
> 'name1' =>
> '%users = map split (/:/, $_, 2), <DATA> ;',
>
> 'name2' =>
> 'while (<DATA>)
> { $Users{substr ($_, 0, index ($_, ":"))} = substr ($_, index ($_, ":") + 1); }',
> } );
> }
This tests NOTHING. As soon as the DATA has been read until the end,
nothing else happens. You should at least have reset the seek value for
this handle to what it was at startup.
--
Bart.
"Godzilla!" <godz...@stomp.stomp.tokyo> rants in
news:3BFB084E...@stomp.stomp.tokyo...
| > > @Associative_Array = qw (user1:pass1:uid1:gid1:gecos1:home1:shell1
| > > user2:pass2:uid2:gid2:gecos2:home2:shell2);
|
| > This is NOT an associative array. It is an array. Learn the
| > difference.
|
| > And now you go back in your hole, you troll.
|
|
| You give up? Appears so. Frustrating, ain't I?
|
| An associative array today is called a hash. For a decade
| or more, it is said, "Call it a hash!" So, this expression
| "associative array" is free for use. Virtually none, actually
| none in this group use the expression associative array
| except for me. I view an array containing elements with
| associated values to be, of all things, an associative
| array.
| All three of you elected
| to respond to the same article of mine all within the
| same temporal period. All three of you reacted with
| identical thinking, identical logic along with identical
| intensity, identical vehemence and many other identical
| personality quirks; personal idioms, such as psychotically
| defending each other. It is as if, it is a certainty,
| all three of you share the same brain, the same thinking,
| the same logic and, the same personality.
Ever considered that it could be just some abstract concept called "common
sense"? Or even "reason"?
| Clever, ain't I? Want to continue playing mind games?
| Around here, I am certainly the best at mind games,
No. You had no argument why it cannot be common sense that makes us tell you
the same.
Steffen
--
$_=q;0cb212c210b0bb010c0113bb0c410c0b516c0bb3d212c2b0b0b016b6cb2b2c21010c0
b41110b3bba0e0c0d2c4b2b6bc013d2c0d0b01012b0b0;;s/\n//g;s/(\d)/$1<2?$1:'0'x
$1/ge;s/([a-f])/'1'x(ord($1)-97)/ge;$o=$_;push@o,substr($o,$_*8,8) for(0..
24);for(@o){print"\0"x(26-$i).chr(oct('0b'.($_)))."\r";$i++};print"\n"#stm
Really? Here is a truly cute little test
script for you. Gosh, you sure love flaunting
how little you know about Perl. You make for
such an easy mark. Kinda funny, your tossing
so many temper tantrums, Sheila... err... Frank.
Godzilla! Queen Of Mind Munchers.
--
#!perl
seek (DATA, 0, 0);
while (<DATA>)
{ print $_; }
__DATA__
>seek (DATA, 0, 0);
>while (<DATA>)
> { print $_; }
>__DATA__
I said "what it was at startup", not 0.
--
Bart.
> This tests NOTHING. As soon as the DATA has been read until the end,
> nothing else happens. You should at least have reset the seek value for
> this handle to what it was at startup.
You are so funny!
"Never Give A Sucker An Even Break."
- W.C. Fields
Here ya go. You and others will note there is no
significant differences in the timing ratio of
Uri's standard issue map method and, my highly
imaginative and eloquent substring method, save
for results being smoothed by effects of opening
and closing a filehandle, repeatedly. Clearly,
accuracy is way diminished doing it your way.
I have an idea for you. This will help with your
frantic fragile masculine ego driven retort.
Post more falsely fabricated benchmark results
using a different fake name of yours, Frank.
Godzilla! Queen Of Belly Laughs.
--
CONTENTS OF TEST.TXT:
_____________________
<HTML>
GARBAGE
<img src="images/large/0010.jpg">
GARBAGE
<img src="images/tn/tn_0010.jpg">
<img src="images/large/0102.jpg">
GARBAGE
<img src="images/tn/tn_0102.jpg">
<img src="images/tn/tn_0215.jpg">
<img src="images/large/0215.jpg">
GARBAGE
</HTML>
BENCHMARK SCRIPT:
_________________
#!perl
print "Content-type: text/plain\n\n";
use Benchmark;
print "Run One:\n\n";
&Time;
print "\n\nRun Two:\n\n";
&Time;
print "\n\nRun Three:\n\n";
&Time;
sub Time
{
timethese (100000,
{
'name1' =>
'open (DATA, "test.txt");
%users = map split (/:/, $_, 2), <DATA> ;
close (DATA);',
'name2' =>
'open (DATA, "test.txt");
while (<DATA>)
{ $Users{substr ($_, 0, index ($_, ":"))} = substr ($_, index ($_, ":") + 1); }
close (DATA);',
} );
}
PRINTED RESULTS:
________________
Run One:
Benchmark: timing 100000 iterations of name1, name2...
name1: 49 wallclock secs (48.56 usr + 0.00 sys = 48.56 CPU) @ 2059.31/s
name2: 47 wallclock secs (46.96 usr + 0.00 sys = 46.96 CPU) @ 2129.47/s
Run Two:
Benchmark: timing 100000 iterations of name1, name2...
name1: 48 wallclock secs (48.39 usr + 0.00 sys = 48.39 CPU) @ 2066.54/s
name2: 46 wallclock secs (47.12 usr + 0.00 sys = 47.12 CPU) @ 2122.24/s
Run Three:
Benchmark: timing 100000 iterations of name1, name2...
name1: 48 wallclock secs (48.50 usr + 0.00 sys = 48.50 CPU) @ 2061.86/s
name2: 46 wallclock secs (47.02 usr + 0.00 sys = 47.02 CPU) @ 2126.75/s
(snipped the funny parts)
> > >Here are some benchmark results which have not been
> > >falsely fabricated. As you know, map methods become
> > >less efficient with larger data samples, Frank.
> > This tests NOTHING. As soon as the DATA has been read until the end,
> > nothing else happens. You should at least have reset the seek value for
> > this handle to what it was at startup.
> Here ya go. You and others will note there is no
> significant differences in the timing ratio of
> Uri's standard issue map method and, my highly
> imaginative and eloquent substring method, save
> for results being smoothed by effects of opening
> and closing a filehandle, repeatedly. Clearly,
> accuracy is way diminished doing it your way.
Oh, oh, oh, my poor aging and addled mind! I have
linked to the wrong data base. No matter, no change.
Results are still smoothly skewed doing this your way.
Godzilla! Queen Of Addleland.
--
CONTENTS OF TEST2.TXT:
______________________
user1:pass1:uid1:gid1:gecos1:home1:shell1
user2:pass2:uid2:gid2:gecos2:home2:shell2
BENCHMARK SCRIPT:
_________________
#!perl
print "Content-type: text/plain\n\n";
use Benchmark;
print "Run One:\n\n";
&Time;
print "\n\nRun Two:\n\n";
&Time;
print "\n\nRun Three:\n\n";
&Time;
sub Time
{
timethese (100000,
{
'name1' =>
'open (DATA, "test2.txt");
%users = map split (/:/, $_, 2), <DATA> ;
close (DATA);',
'name2' =>
'open (DATA, "test2.txt");
while (<DATA>)
{ $Users{substr ($_, 0, index ($_, ":"))} = substr ($_, index ($_, ":") + 1); }
close (DATA);',
} );
}
PRINTED RESULTS:
________________
Run One:
Benchmark: timing 100000 iterations of name1, name2...
name1: 42 wallclock secs (41.97 usr + 0.00 sys = 41.97 CPU) @ 2382.65/s
name2: 41 wallclock secs (40.64 usr + 0.00 sys = 40.64 CPU) @ 2460.63/s
Run Two:
Benchmark: timing 100000 iterations of name1, name2...
name1: 41 wallclock secs (41.69 usr + 0.00 sys = 41.69 CPU) @ 2398.66/s
name2: 39 wallclock secs (40.75 usr + 0.00 sys = 40.75 CPU) @ 2453.99/s
Run Three:
Benchmark: timing 100000 iterations of name1, name2...
name1: 40 wallclock secs (41.69 usr + 0.00 sys = 41.69 CPU) @ 2398.66/s
name2: 41 wallclock secs (40.70 usr + 0.00 sys = 40.70 CPU) @ 2457.00/s
Godzilla!> Dale Henderson wrote:
>> So what's the difference between a hash and and associative
>> array?
Godzilla!> My suggestion is you research and read about arrays and
Godzilla!> hashes. It is clear you have a bit of Perl learning
Godzilla!> yet to accomplish; differences between arrays and
Godzilla!> hashes, are readily apparent.
Actually, I've programmed perl for a number of years and even did
it professionally for a while.
My intent in asking this question was to find out what you
thought the difference between and _associative_ array and hash
was. As the terms are used interchangeably by most perl
programmers.
Notice that I did not respond with: "Associative
arrays and Hashes are the same thing. You obviously need to learn
more about perl." Instead I asked for clarification of the point
you were trying to make.
I've the other posts you've made in this thread and you still
haven't given a clear answer to the question I posed above. Only
vague allusions. The nearest I can come up with is that you may
mean something similar to a lisp alist i.e.:
@alist=(['key1','value1'],['key2','value2']);
But that would be less efficient than a hash.
You obviously don't know Godzilla yet. Ignore anything it says. See
signature and Google for reasons why. It is pointless to get into
discussions with it, as you should have guessed from the response you
got, and that other posts in this thread got.
Martien
--
Do not pay any attention to what Godzilla says. It is a troll, and has
no decent working knowledge of Perl or programming in general. Search
groups.google.com to see a history of its posts and replies to these posts.
(snipped)
> Actually, I've programmed perl for a number of years and even did
> it professionally for a while.
I have tiny winged blue monkeys flying out of my big butt,
each harmoniously singing Steppenwolf's "Born To Be Wild."
Godzilla!
--
http://la.znet.com/~callgirl3/bornwild.mid
You horrible troll! You posted that exact phrase before.
My answer: (2001-08-12 23:41:02 PST)
> > my big butt
>
> Cute. That's what I thought.
>
> How many elephants do you have sitting in your butt that sing Creed's "My
> Own Prison"?
[snipped]
Please, folks, do not feed the troll.
I'm not saying anything against the occasional public-service warning
being posted. But taunting is futile: it just raises the general
noise level, and makes the group less-useful for everyone.
f'ups set.
> Bart Lateur wrote:
>
> > Godzilla! wrote:
>
> > >> So what's the difference between a hash and and associative
> > >> array?
>
> > >Format, creation and access methodologies along with efficiency.
>
> > You stupid person. A "hash" is simply another shorter name for an
> > "associative array". Technically, they're the same thing.
>
> > So what's the difference between a car and an automobile?
>
> You state, with clarity, an associative array and a hash
> are the same, identical, no difference between the two.
> This you have stated, without any doubt.
He stated it with all the clarity, correctness, urgency and deliberation
that the truthfulness of his statement warranted. :)
An "associative array" got its name, because, unlike a regular array, it
associates a key with a value:
$simpletons{'godzilla'} = 'not understanding hashes';
Whereas in a regular array:
push (@simpletons, 'godzilla');
There is nothing to associate the element with; except, of course, the
temporary laughter elicited by the memory of your studious display of
failing to grasp even the most basic perl fundamentals. But, while amusing
to watch, hardly makes this array associative. :)
- Mark
Drew,
In addition to the multiple posts already provided, consider the
Unix::PasswdFile module. Available on CPAN.
Regards,
Mr. Sunblade.
I don't know that there is a well-defined difference between them, but
if I had to come up with a definition, I'd say that an associative
array is a programming language concept and a hash is an algorithm for
implementing it.
There are other languages besides Perl[1] that have associative arrays,
and in theory they could use something other than a hash to implement
them (like a binary search tree). Really, a Perl implementation could
use something other than a hash as the implementation if it wanted to,
and if such a thing ever happened then the term "hash" would become
a bit (more?) of a misnomer.
- Logan
[1] Like awk, JavaScript, and I'm sure others, although I don't
know what they are.
--
"In order to be prepared to hope in what does not deceive,
we must first lose hope in everything that deceives."
Georges Bernanos
my ( $user, $other ) = split /:/, $_, 2;
$users{ $user } = $other;
> }
> close (EP) || die "Can't fork close EP: $!\n";
> return \%users;
> }
>
> Effectively, I'd like to combine the split and join statements above,
> but I'm not sure exactly how to do that, or if it's even a viable
> solution.
John
--
use Perl;
program
fulfillment
> > So what's the difference between a hash and and associative array?
> I don't know that there is a well-defined difference between them, but
> if I had to come up with a definition, I'd say that an associative
> array is a programming language concept and a hash is an algorithm for
> implementing it.
> There are other languages besides Perl[1] that have associative arrays,
> and in theory they could use something other than a hash to implement
> them (like a binary search tree). Really, a Perl implementation could
> use something other than a hash as the implementation if it wanted to,
> and if such a thing ever happened then the term "hash" would become
> a bit (more?) of a misnomer.
Well, well, one of you boys have actually added something intelligent.
* believed those boys would never arrive at this realization *
Here is code exemplifying use of a true associative array.
It is not as efficient as its equal in hash format. Still,
this might inspire some intelligent extrapolation regarding
efficiency of complex associative arrays and complex hashes.
#!perl
@Fruits = qw (apple plum grape orange);
$user_input = "Fruits";
print "@$user_input";
* not surprised none realized this earlier despite my previous examples of this *
What logical defining term would you tag to this array, @Menu?
@Menu = ("@Meat", "@Vegetables", "@Salads", "@Fruits");
$user_input = 2;
print "$Menu[$user_input]";
Godzilla!
> Here is code exemplifying use of a true associative array.
> It is not as efficient as its equal in hash format. Still,
> this might inspire some intelligent extrapolation regarding
> efficiency of complex associative arrays and complex hashes.
>
>
> #!perl
>
> @Fruits = qw (apple plum grape orange);
>
> $user_input = "Fruits";
>
> print "@$user_input";
ethan@ethan:~$ perl -Mstrict
my @Fruits = qw (apple plum grape orange);
my $user_input = "Fruits";
print "@$user_input";
__END__
Can't use string ("Fruits") as an ARRAY ref while "strict refs" in use
at - line 3.
A true associative array, eh?
Better come back with a true associative array working under strictures
or return to your sandbox.
> What logical defining term would you tag to this array, @Menu?
[...]
Something that screws your symbol-table and hence is not allowed under
strict.
Tassilo
--
/*
* Please skip to the bottom of this file if you ate lunch recently
* -- Alan
*/
-- from Linux kernel pre-2.1.91-1
> > #!perl
> > @Fruits = qw (apple plum grape orange);
> > $user_input = "Fruits";
> > print "@$user_input";
> ethan@ethan:~$ perl -Mstrict
> my @Fruits = qw (apple plum grape orange);
> my $user_input = "Fruits";
> print "@$user_input";
> __END__
> Can't use string ("Fruits") as an ARRAY ref while "strict refs" in use
> at - line 3.
> A true associative array, eh?
> Better come back with a true associative array working under strictures
> or return to your sandbox.
> Something that screws your symbol-table and hence is not allowed under
> strict.
Oh look, I smoked another Taliban Cleric Code Cop out of his
Perl 5 Cargo Cultist cave.
Clearly you invested a lot of time and effort into finding
a way to break my code, although you have to employ deceit
to do so. I am impressed with your intelligent dialog,
truly I am.
As I stated in another article, you boys are trolls, Frank.
Godzilla! Queen Of Perl Heretics.
(snipped)
> > > Here is code exemplifying use of a true associative array.
> > Can't use string ("Fruits") as an ARRAY ref while "strict refs" in use
> > at - line 3.
> > Better come back with a true associative array working under strictures
> > or return to your sandbox.
> > Something that screws your symbol-table and hence is not allowed under
> > strict.
> Oh look, I smoked another Taliban Cleric Code Cop out of his
> Perl 5 Cargo Cultist cave.
With my having smoked you out of your Perl 5 Cargo Cultist cave...
* grabs the loose end of his Taliban Cleric Code Cop turban *
MUUUUHAHAHAHAHAAAA! ALLAH IS GREAT!
* takes off running *
Boy Howdy! Will ya look at him spin like a top!
Here's something which will make you even more dizzy.
This code exemplifies a talent which differentiates
real programmers from Perl 5 Cargo Cultists such
as yourself. This is a talent to write imaginative
scripts which do what we, real programmers, want
rather than what Perl 5 Cargo Cult Dogma dictates
you will do with a script.
Obviously you don't know much about strict and its
features, which are incorporated for many reasons,
with those reasons being beyond simple comprehension
of stereotypical Perl 5 Cargo Cultists.
As a side effect, this code exposes your deceit
through concealment of truth and, how little you
know about Perl, Frank.
Godzilla! Queen Of Muuhaha.
--
TEST SCRIPT:
____________
#!perl
use strict;
our (@Fruits) = qw (apple plum grape orange);
our ($user_input) = "Fruits";
no strict "refs";
print "@$user_input\n\n";
use strict "refs";
print "@$user_input";
exit;
PRINTED RESULTS:
________________
apple plum grape orange
Can't use string ("Fruits") as an ARRAY ref while "strict refs"
in use at test.pl line 15.
Moronic.
Tassilo
--
The mother of the year should be a sterilized woman with two adopted children.
-- Paul Ehrlich
Tassilo wrote:
> > A true associative array, eh?
> > Better come back with a true associative array working under
> > strictures or return to your sandbox.
>
> > Something that screws your symbol-table and hence is not
> > allowed under strict.
>
>
> Oh look, I smoked another Taliban Cleric Code Cop out of his
> Perl 5 Cargo Cultist cave.
>
> Clearly you invested a lot of time and effort into finding
> a way to break my code, ....
Investing a lot of time into breaking your code?? All he had to do was add
"use strict;" to buckle the hull! And, in the true Perl spirit, "There is
more than one way to break your code." :)
But, seriously, if your mind is so prone to taking long leaves of absence,
perhaps you should consider a vacation program.
- Mark