[22634] in Perl-Users-Digest
Perl-Users Digest, Issue: 4855 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 16 18:05:56 2003
Date: Wed, 16 Apr 2003 15:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 16 Apr 2003 Volume: 10 Number: 4855
Today's topics:
Can I die and also write to a log file? (Sherman Willden)
Re: Can I die and also write to a log file? <noreply@gunnar.cc>
Re: Can I use a hash slice through an anonymous hashref <uri@stemsystems.com>
Re: Can I use a hash slice through an anonymous hashref <bart.lateur@pandora.be>
CGI Error "unitialized constant" Help <mchyzer@yahoo.com>
Re: CGI Error "unitialized constant" Help (Tad McClellan)
conditional 'use' <mark.seger@hp.com>
Difficult search for similar filename in current direct (Tom Parson)
Re: Difficult search for similar filename in current di <w.koenig@acm.org>
Re: How does Perl auto increase hash size? ctcgag@hotmail.com
Re: How does Perl auto increase hash size? ctcgag@hotmail.com
Matching two patterns at once <sbour@niaid.nih.gov>
Re: Matching two patterns at once <noreply@gunnar.cc>
Re: Matching two patterns at once <prakash@resp-sci.arizona.edu>
Re: need finding information <urzaserra@home.com>
Re: newbie:substitution in a variable name (Jay Tilton)
Number of List Items <delautenschl@[nospam]wisc.edu>
Re: Number of List Items <fxn@hashref.com>
Re: Number of List Items (Tad McClellan)
Re: Number of List Items (Tad McClellan)
Re: Number of List Items (Jay Tilton)
Pushing objects onto an array (Erick Bourgeois)
Re: Pushing objects onto an array <uri@stemsystems.com>
Re: Pushing objects onto an array (Jay Tilton)
Re: regexp. for a two dates delimited with a minus <laocoon@fastmail.fm>
Re: regexp. for a two dates delimited with a minus <noreply@gunnar.cc>
Re: Remembering previous values of the form. <infinite.possibilities@NOSPAMatt.net>
Re: Remote Module Installation <ericw@nospam.ku.edu>
Re: Stumped! <nobull@mail.com>
Re: Stumped! <delautenschl@[nospam]wisc.edu>
Re: Stumped! <delautenschl@[nospam]wisc.edu>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 16 Apr 2003 12:41:04 -0700
From: sherman.willden@hp.com (Sherman Willden)
Subject: Can I die and also write to a log file?
Message-Id: <3a80d8d6.0304161141.260da490@posting.google.com>
Can I use die and also write to a log file? I am using perl perl,
v5.6.1 built for MSWin32-x86-multi-thread
I quit using die because I had to also write to a log file. I am also
generating a message that indicates the script and the line number as
shown in _Progamming Perl, 2nd Edition_, page 157. So I open an error
log (EL) and write to it like the if statement shown below.
if ( "$DIR" eq "" ) {
print '"', __FILE__, '", line, ', __LINE__, ", Unknown directory
$DIR\n";
print EL '"', __FILE__, '", line, ', __LINE__, ", Unknown
directory $DIR\n";
print "Exiting $0\n";
print EL "Exiting $0\n";
close(EL);
exit 1;
}
This results in the below message:
"C:\BuildScripts\V2.1A\Windows\TryExit.pl", line, 15, Unknown
directory C:\mydir
Is there an easier way?
Thanks;
Sherman
------------------------------
Date: Wed, 16 Apr 2003 22:53:32 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Can I die and also write to a log file?
Message-Id: <b7kga7$1viq1$1@ID-184292.news.dfncis.de>
Sherman Willden wrote:
> Can I use die and also write to a log file?
Your question makes me think of the carpout method in CGI::Carp.
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 16 Apr 2003 18:58:17 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Can I use a hash slice through an anonymous hashref?
Message-Id: <x77k9ujmlz.fsf@mail.sysarch.com>
>>>>> "S" == Sara <genericax@hotmail.com> writes:
S> thanks. It seems sort of strange that we write
S> $href->{cat}=1; # for a hashref
S> $href{cat} =1; # for a hash
the latter shouldn't be called href as it is a real hash and not a ref.
S> but for slices, the -> vanishes. Yes what you provided was a big help,
you aren't separating the fact that a ref is a scalar and a hash is an
aggregate.
you can use a has ref anywhere you see a hash name.
$$href{cat} = 1 ;
that is legal but hard to read. so the -> is syntactical sugar for
dereferencing.
$href->{cat} = 1 ;
you can even use a real hash in that just by getting its reference:
(\%hash)->{cat} = 1 ;
(not sure about the precedence of \ and i am too lazy and don't care to
use it so i didn't look it up or try it).
that is kinda silly but it is legal. so you can use a hash where you
want a ref and a ref where you want a hash. but you must use the correct
syntax to show it.
here is why. what if you had:
$hash = {} ;
%hash = () ;
$hash{foo} = 1 ;
which did it use, $hash or %hash? the rule is you use a $ sigil to mark
a hash is being used in a scalar context when you access a single
element. so that is %hash. so you need differen syntax to use a hash
ref.
now, in perl6, you ALWAYS use % for hashes no matter how they are
used. so this now works:
$hash = {} ;
%hash = () ;
$hash{foo} = 1 ; # this is using $hash!!
# perl6 will auto-derefence it for you.
%hash{foo} = 1 ; # this is using %hash
by forcing the sigil to always be %, you can use hash refs like hash
names. the only catch is you can't have any white space before the {}
part anymore. that is so perl6 can drop the parens around the
conditional in if/while statements.
all in all it is a major improvement over perl5 once you get used to
it. took me a while to get that but i am fully on board that change.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 16 Apr 2003 19:23:38 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Can I use a hash slice through an anonymous hashref?
Message-Id: <p9br9v06djnmpvghj8cmdhsqvh1pjahatr@4ax.com>
Sara wrote:
> It seems sort of strange that we write
>
> $href->{cat}=1; # for a hashref
> $href{cat} =1; # for a hash
>
>but for slices, the -> vanishes.
Yes and no. The -> is a special syntax that, unfortunately cannot be
used for slices. But you can still access a single item in a hashref in
the same syntax as the slices, so the "->" syntax is the special case,
really:
@{$h}{@keys} = @values;
${$h}{$key} = $value;
The latter is the same as
$h->{$key} = $value;
--
Bart.
------------------------------
Date: Wed, 16 Apr 2003 18:10:35 GMT
From: "Hyzer" <mchyzer@yahoo.com>
Subject: CGI Error "unitialized constant" Help
Message-Id: <v0hna.21713$NY1.19276@nwrddc04.gnilink.net>
Hello,
I am running a perl powered website, and I keep getting this error for
various places in the code.
[Wed Apr 16 19:39:45 2003] null: Use of uninitialized value in concatenation
(.) at Gizmo/Table.pm line 615.
I checked this particular case, and there is no way that the variable is
unitialized, and there is no concatenation on that line. From other
newsgroups it says to ignore these errors, but occasionally the website isnt
generated the pages correctly, so I figured I would look into it more.
Any ideas?
Thanks,
Chris
ps. here is the line:
if ((defined $noorder) && ($noorder == 0)) #CH 030416 not (!($noorder))
------------------------------
Date: Wed, 16 Apr 2003 16:28:20 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: CGI Error "unitialized constant" Help
Message-Id: <slrnb9rink.47g.tadmc@magna.augustmail.com>
Hyzer <mchyzer@yahoo.com> wrote:
> Subject: CGI Error "unitialized constant" Help
^^^^^^^^^^^^^^^^^^^^
There is no such Perl message...
> [Wed Apr 16 19:39:45 2003] null: Use of uninitialized value in concatenation
> (.) at Gizmo/Table.pm line 615.
>
> I checked this particular case, and there is no way that the variable is
> unitialized,
Yes there is, you just aren't seeing it.
Humans make mistakes a whole lot more often than machines do.
If the machine notices undef's, then you most certainly do have
undefs somewhere.
> and there is no concatenation on that line.
Interpolation is concatenation.
Are you using double quoted strings?
> From other
> newsgroups it says to ignore these errors,
Then the people in those newgroups are utter fools.
> but occasionally the website isnt
> generated the pages correctly, so I figured I would look into it more.
>
> Any ideas?
> ps. here is the line:
>
> if ((defined $noorder) && ($noorder == 0)) #CH 030416 not (!($noorder))
You are using undef somewhere in the if-block, but we can't show
you where because you did not show the code that is in the block.
Perl does the best that it can when reporting line numbers, but
sometimes its best is not as good as we would like.
Your problem is somewhere else.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 16 Apr 2003 15:04:23 -0400
From: Mark Seger <mark.seger@hp.com>
Subject: conditional 'use'
Message-Id: <3E9DA937.32029E07@hp.com>
I'm sure this is easy enough to do if you've already done it before, but
I haven't and so am not sure how.
Basically I have a script that uses 'Compress::Zlib' when it's there but
I don't want to use it when it's not. Since you can't conditionalize a
"use", I assume you use a "require" instead.
Using a 'use' statement to load Zlib, all I need to do is:
use Compress::Zlib;
$Z=gzopen();
$Z->gzwrite();
$Z->gzclose();
when I put in a "require" pointing to Compress/Zlib.pm instead, all I
did was change my open call to:
$Z=Compress::Zlib->gzopen();
and it all seemed to work. Is this indeed the right way to do it? is
there addiitional 'stuff' required?
I'm also not sure where is the best place to look for installed
modules. If I look at $Config{} there are a number of directories
mentioned that seem like candidates, the most promising being
"sitearch". "sitearchexp" and "installsitearch" also point to the
location of Compress as well. Do all modules get installed in
$Config{"sitearch"} by default?
-mark
------------------------------
Date: Wed, 16 Apr 2003 22:12:11 +0200
From: tom.parson@gm.com (Tom Parson)
Subject: Difficult search for similar filename in current directory ??? /pattern matching
Message-Id: <b7kdep$lto$01$1@news.t-online.com>
Assume I have a filename in $filename
Now I want to check if a file in the same directory exists whose filename is identical except
one character. Read where the the length of the filename is identical and I only to need to
replace one arbitrary character of a given filename to get another existing file.
How do I code this search ?
I should be something like
if (-e replaceonecharacter($filename)) {
print "Bingo\n"; }
Regards
Tom
------------------------------
Date: Wed, 16 Apr 2003 22:52:29 +0200
From: Winfried Koenig <w.koenig@acm.org>
Subject: Re: Difficult search for similar filename in current directory ??? /pattern matching
Message-Id: <3E9DC28D.7020606@acm.org>
Tom Parson wrote:
> Assume I have a filename in $filename
>
> Now I want to check if a file in the same directory exists whose filename is identical except
> one character. Read where the the length of the filename is identical and I only to need to
> replace one arbitrary character of a given filename to get another existing file.
>
> How do I code this search ?
>
> I should be something like
>
> if (-e replaceonecharacter($filename)) {
> print "Bingo\n"; }
>
> Regards
> Tom
Try this:
$filename = "some_name";
$dir = "path_to_directory";
opendir(DIR, $dir) or die "Can't open $dir; $!\n";
while (defined(my $file = readdir(DIR))) {
next unless length($filename) == length($file);
my $diff = $file ^ $filename;
my $difflength = $diff =~ tr/\000//c;
if ($difflength == 1) {
print "Found: $file\n";
}
}
closedir DIR;
Winfried Koenig
------------------------------
Date: 16 Apr 2003 18:11:33 GMT
From: ctcgag@hotmail.com
Subject: Re: How does Perl auto increase hash size?
Message-Id: <20030416141133.189$36@newsreader.com>
"Steffen Beyer" <Steffen.Beyer@de.bosch.com> wrote:
> Abigail wrote:
>
> > `' Of course. But this also makes the result of the hashing function
> > `' depend on the size of the hash, doesn't it?
> >
> > Yes.
Depends on what you call *the* hash function. My understanding
is that, in effect, real_hash($string,$buckets) is
defined as:
virtual_hash($string) % $buckets;
So one of these depends on number of buckets, one doesn't. (This would
imply an absolute limit to the number of buckets, the maximum value
the virtual_hash function can return).
>
> > `' So when you increase the size, you do not only have to recalculate
> > `' the hash function for each entry, you also have to move the entry
> > `' to its new place, indicated by the new hash function - right?
> >
> > Yes. (Note that part of the calculation are cached).
>
> How so?
The results of virtual_hash are (or could be) remembered, so only the
modulo part needs to be redone.
>
> > `' How is this moving done efficiently?
> > `' Through reassigning pointers?
> >
> > Yes.
>
> Nevertheless, this seems like a waste of valuable time.
My time is valuable. The computer's time...not so valuable.
> Is there no way to leave the hash elements in place and
> extend the hash function in some clever way?
>
> Like, using the first hash function as the first probe,
> and in case of conflict, the second (new) hash function?
> (This is some sort of double hashing, then, of course)
>
> Of course this would imply a list of hash functions after
> a couple of resizing steps. But if resizing always doubles
> the size of the hash table, the time complexity for this
> is only O( log2(n) ).
The log2(n) time complexity for that methods exists for every access.
The time complexity for rehashing is about 1 rehashing of each of n
elements done every log2(n)/n inserts, which amortizes to log2(n),
and is only paid on inserts, not look-ups.
The re-hashing can be avoided by pre-allocating hash buckets, but
I've tested this several times and never seen it make a worthwhile
difference, so I have to assume it's really a non-issue to start with.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
------------------------------
Date: 16 Apr 2003 21:07:24 GMT
From: ctcgag@hotmail.com
Subject: Re: How does Perl auto increase hash size?
Message-Id: <20030416170724.115$OQ@newsreader.com>
ctcgag@hotmail.com wrote:
>
> The re-hashing can be avoided by pre-allocating hash buckets, but
> I've tested this several times and never seen it make a worthwhile
> difference, so I have to assume it's really a non-issue to start with.
I've just retested this, and preallocating buckets does make a huge
difference, just not where one would expect.
If I "my" a hash, adding one million things to it takes 12 seconds.
Destroying the hash when the program ends takes 100 seconds.
If I "my" a hash and preallocate buckets (doesn't matter how many), then
add one million things to it, it still takes 12 seconds. But Destroying
the hash when the program ends is now instantaneous.
Even if I preallocate only 8 buckets (which is the number a hash starts
with anyway), I still get the huge speed-up on destruction.
I get this under both 5.6.0 and 5.6.1 on linux, usemyalloc=n.
Xho
#!/usr/bin/perl -wl
use strict;
my %hash;
# keys %hash=1_000_000; # or 1000, or 8, or comment this line out
altogether.
$hash{0}=1; # doesn't tell you how many buckets there
# are until at least one thing is there.
print timer();
print scalar %hash;
foreach (1..1_000_000) {
$hash{("x"x1000).$_}=1;
};
print scalar %hash;
print timer();
{ my $last_time;
sub timer {
unless (defined $last_time) {
$last_time=time();
return "First usage";
};
my $return = time() - $last_time;
$last_time=time();
return $return;
};
};
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
------------------------------
Date: Wed, 16 Apr 2003 17:01:48 -0400
From: Stephan Bour <sbour@niaid.nih.gov>
Subject: Matching two patterns at once
Message-Id: <BAC33CFC.74CC%sbour@niaid.nih.gov>
I have a multi-line output text that contains pertinent information between
two different patterns. I'm trying to split the string to build an array
that would contain the text comprised between the two search patterns. To
illustrate: if I have the string "I don't know much about perl" and I want
to extract "much about" looking for anything comprised between "know" and
"perl", can I do that in one go or do I need two splits?
This doesn't work properly:
@split = split ((/know/ and /perl/), $output);
Thanks for any help,
Stephan.
------------------------------
Date: Wed, 16 Apr 2003 23:00:57 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Matching two patterns at once
Message-Id: <b7kgo5$20t4i$1@ID-184292.news.dfncis.de>
Stephan Bour wrote:
> I have a multi-line output text that contains pertinent information between
> two different patterns. I'm trying to split the string to build an array
> that would contain the text comprised between the two search patterns. To
> illustrate: if I have the string "I don't know much about perl" and I want
> to extract "much about" looking for anything comprised between "know" and
> "perl", can I do that in one go or do I need two splits?
>
> This doesn't work properly:
>
> @split = split ((/know/ and /perl/), $output);
@split = split /know|perl/, $output;
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 16 Apr 2003 14:42:02 -0700
From: Jayaprakash Rudraraju <prakash@resp-sci.arizona.edu>
Subject: Re: Matching two patterns at once
Message-Id: <Pine.GSO.4.10.10304161439380.17913-100000@o2.resp-sci.arizona.edu>
On Wed, 16 Apr 2003, Stephan Bour wrote:
> I have a multi-line output text that contains pertinent information between
> two different patterns. I'm trying to split the string to build an array
> that would contain the text comprised between the two search patterns. To
> illustrate: if I have the string "I don't know much about perl" and I want
> to extract "much about" looking for anything comprised between "know" and
> "perl", can I do that in one go or do I need two splits?
>
> This doesn't work properly:
>
> @split = split ((/know/ and /perl/), $output);
($text) = ($output =~ /know(.*)perl/);
>
> Thanks for any help,
> Stephan.
>
>
>
------------------------------
Date: Wed, 16 Apr 2003 11:18:07 -0700
From: "matt" <urzaserra@home.com>
Subject: Re: need finding information
Message-Id: <g6hna.32$GD6.10@fed1read02>
"matt" <urzaserra@home.com> wrote in message
news:%5Yma.40$Gv5.39@fed1read02...
> I looked at perldoc.com but could not locate any infromation about
new-line
> charachters for different oses and such. Would not having the right new
> line charachters cause premature end of script headers error??
>
Silly me i figured out what this error means. It means that the shebang
line is wrong.
------------------------------
Date: Wed, 16 Apr 2003 21:47:50 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: newbie:substitution in a variable name
Message-Id: <3e9dcf7a.99613314@news.erols.com>
Barry Kimelman <barryk2@SPAM-KILLER.mts.net> wrote:
: In article <b7gvq8$q7ni$1@ID-155280.news.dfncis.de>, Alexander Eisenhuth
: (stacom@stacom-software.de) says...
: > Hallo,
: >
: > how can I access the value of these variables in a loop:
: >
: > my $B_01MASK = 0x01;
: > my $B_02MASK = 0x02;
: > my $B_03MASK = 0x04;
: > my $B_04MASK = 0x08;
: > my $B_05MASK = 0x10;
: > my $B_06MASK = 0x20;
: > my $B_07MASK = 0x40;
: > my $B_08MASK = 0x80;
:
: $varname = "B_0" . $i . "MASK";
: print "Value of $varname is : ",$$varname,"\n";
Along with the standard cautions against symrefs, they cannot be used
to access the OP's lexical variables.
------------------------------
Date: Wed, 16 Apr 2003 15:27:03 -0500
From: pooba53 <delautenschl@[nospam]wisc.edu>
Subject: Number of List Items
Message-Id: <b7keak$b01$1@news.doit.wisc.edu>
I have a list that contains references. I'm trying to find out how many
items are in the list.
Usually, if the list is @some_list, you would say:
$count = $#some_list but this does not work with a list of references.
My list is -----> @{$unique[0]}
-Dan
------------------------------
Date: Wed, 16 Apr 2003 21:34:26 +0000 (UTC)
From: Xavier Noria <fxn@hashref.com>
Subject: Re: Number of List Items
Message-Id: <b7ki92$rk4$1@news.ya.com>
In article <b7keak$b01$1@news.doit.wisc.edu>, pooba53 wrote:
: I have a list that contains references. I'm trying to find out how many
: items are in the list.
:
: Usually, if the list is @some_list, you would say:
:
: $count = $#some_list but this does not work with a list of references.
Wrong, you would say
$count = @some_list;
and that works always, the fact that some elements are references is
irrelevant for that matter.
If you have a list of nested arrayrefs and want to count all the scalars
which are not arrayrefs flattening them this could serve:
sub deep_count {
my ($aref) = @_;
my $count = 0;
$count += ref eq 'ARRAY' ? deep_count($_) : 1 foreach @$aref;
return $count;
}
Given
my @foo = ([1, [2, [3, 4]]], [5, [6, 7, 8 ,9], []]);
deep_count(\@foo) would return 9.
And given
my @bar = ([], [], []);
deep_count(\@bar) would return 0.
-- fxn
------------------------------
Date: Wed, 16 Apr 2003 17:39:29 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Number of List Items
Message-Id: <slrnb9rmt1.4e7.tadmc@magna.augustmail.com>
pooba53 <delautenschl@[nospam]wisc.edu> wrote:
> I have a list that contains references. I'm trying to find out how many
> items are in the list.
It is often not possible to find out how many items are in a list.
> Usually, if the list is @some_list,
That is NOT a list. That is an array.
Have you seen this Perl FAQ?
What is the difference between a list and an array?
> you would say:
>
> $count = $#some_list
That does NOT tell you how many items are in the array, it tells
you the last index of the array.
$count = @some_list;
_That_ tells you how many items are in the array.
> but this does not work with a list of references.
Yes it does (though it would be off-by-one).
--------------------------------
#!/usr/bin/perl
use strict;
use warnings;
my @some_list = ( [1], [2], [3], [4], [5] );
my $count = @some_list;
print "there are $count items in \@some_list\n";
--------------------------------
output:
there are 5 items in @some_list
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 16 Apr 2003 17:42:49 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Number of List Items
Message-Id: <slrnb9rn39.4eo.tadmc@magna.augustmail.com>
pooba53 <delautenschl@[nospam]wisc.edu> wrote:
> I'm trying to find out how many
> items are in the list.
> My list is -----> @{$unique[0]}
It works for me:
-------------------------
#!/usr/bin/perl
use strict;
use warnings;
my @unique = ( [0,1,2,3] );
my $count = @{$unique[0]};
print "there are $count items in \@some_list\n";
my $index = $#{$unique[0]};
print "the last index of \@some_list is $index\n";
-------------------------
output:
there are 4 items in @some_list
the last index of @some_list is 3
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 16 Apr 2003 21:50:26 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Number of List Items
Message-Id: <3e9dcfaa.99661600@news.erols.com>
pooba53 <delautenschl@[nospam]wisc.edu> wrote:
: I have a list that contains references. I'm trying to find out how many
: items are in the list.
:
: Usually, if the list is @some_list,
That's an array, not a list. This is not an insignificant
disctinction. Using the correct terminology is crucial in accurately
conveying your meaning.
See perlfaq4, "What is the difference between a list and an array?"
: you would say:
:
: $count = $#some_list
No, that gives the index of the last element in the array. To get the
number of elements, use the array in a scalar context.
$count = @some_list;
: but this does not work with a list of references.
^^^^^^^^^^^^^
See the subsection titled "Beware of saying 'doesn't work'" in the
clpm posting guidelines. That statement does nothing to explain the
problem.
That the elements of the array are references has no impact at all on
the syntax for counting them.
: My list is -----> @{$unique[0]}
Perhaps you mean,
I have an array of array references. How can I count the
number of elements in one of those referenced arrays?
If that's it, simply dereference and use it in scalar context.
$count = @{$unique[0]};
------------------------------
Date: 16 Apr 2003 11:54:14 -0700
From: erick@jeb.ca (Erick Bourgeois)
Subject: Pushing objects onto an array
Message-Id: <3ecefa21.0304161054.786375da@posting.google.com>
I'm trying to push objects created with the new pragma in a loop.
However, when I read the array after the loop, the only object
I can access (with the proper methods) is the last one pushed on.
Furthermore, I do know that each object pushed does in fact have
a different address. For example,
(presuming Foo::Bar::id() exists and returns the contructor's
original initializer, in this case $_, i.e. 0..5)
use Foo::Bar;
my @array;
for (0..5)
{
my $obj = new Foo::Bar($_);
push(@array, $obj);
# printing $obj produces all different memory addresses
}
for (0..5)
{
my $o = $array[$_];
my $id = $o->id();
# printing $o produces all different memory addresses
# HOWEVER, printing $id is always 5
}
Thank you for any responses,
erick
-------------
$ perl --version
This is perl, v5.6.1 built for i386-linux
------------------------------
Date: Wed, 16 Apr 2003 19:07:14 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Pushing objects onto an array
Message-Id: <x71y02jm72.fsf@mail.sysarch.com>
>>>>> "EB" == Erick Bourgeois <erick@jeb.ca> writes:
EB> I'm trying to push objects created with the new pragma in a loop.
that is a class, not a pragma. pragmas are used only for compiler
related hints and such like strict, constant, etc.
EB> (presuming Foo::Bar::id() exists and returns the contructor's
EB> original initializer, in this case $_, i.e. 0..5)
presuming is the problem.
EB> use Foo::Bar;
EB> my @array;
EB> for (0..5)
EB> {
EB> my $obj = new Foo::Bar($_);
my $obj = Foo::Bar->new($_);
use direct method calls as they are safer. and IMNSHO much easier to read.
EB> push(@array, $obj);
EB> # printing $obj produces all different memory addresses
EB> }
EB> for (0..5)
EB> {
EB> my $o = $array[$_];
EB> my $id = $o->id();
EB> # printing $o produces all different memory addresses
that means you have separate objects which is what you want.
EB> # HOWEVER, printing $id is always 5
that means your class constructor is broken.
since you didn't show that code, we can't debug it. what made you think
it was the push loop that caused the problem? how could push affect the
object? this would have been a simple test for you to do:
my $obj1 = Foo::Bar->new(1);
my $obj2 = Foo::Bar->new(2);
print $obj1->id(), $obj2->id(), "\n" ;
no push, no loops and probable proof that your class is buggy.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 16 Apr 2003 21:58:11 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Pushing objects onto an array
Message-Id: <3e9dd1aa.100173313@news.erols.com>
erick@jeb.ca (Erick Bourgeois) wrote:
: I'm trying to push objects created with the new pragma in a loop.
: However, when I read the array after the loop, the only object
: I can access (with the proper methods) is the last one pushed on.
: Furthermore, I do know that each object pushed does in fact have
: a different address. For example,
:
: (presuming Foo::Bar::id() exists and returns the contructor's
: original initializer, in this case $_, i.e. 0..5)
Is that presumption valid? The available evidence suggests that it is
not.
: my @array;
:
: for (0..5)
: {
: my $obj = new Foo::Bar($_);
: push(@array, $obj);
: # printing $obj produces all different memory addresses
How about printing $obj->id() here to make sure the constructor is
doing the reasonable thing?
: }
: for (0..5)
: {
: my $o = $array[$_];
: my $id = $o->id();
: # printing $o produces all different memory addresses
: # HOWEVER, printing $id is always 5
: }
# dummy Foo::Bar class added to the above code
package Foo::Bar;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
bless({ id => $_[0] }, $class);
}
sub id {
my $self=shift;
$self->{id} = shift if @_;
$self->{id};
}
I'm just not seeing it happen. Either one of the methods is
misbehaving or you're misinterpreting its usage documentation (like,
say, using id() as an object method when it's really a class method).
Either way, without seeing the real Foo::Bar code, this problem is
impossible to diagnose.
I would first lay suspicion on the id() method.
------------------------------
Date: Wed, 16 Apr 2003 20:54:57 +0200
From: Lao Coon <laocoon@fastmail.fm>
Subject: Re: regexp. for a two dates delimited with a minus
Message-Id: <Xns935FD4F055E58laocoon@62.153.159.134>
"Fizgig" <FizgigNO@SPAMequanimityREMOVETHISTO.nl> wrote in
news:3e9d5259$0$49109$e4fe514c@news.xs4all.nl:
> Thank you for your reply Lao. Unfortunatly i need a complete regular
> expression because the way i want to use it doesn't allow me to enter
> other code but a reg expr.
That sounds like a rather special requirement. Might I ask why you can't
run anything but the regex (where then do you get the string to check etc?)
Lao
------------------------------
Date: Wed, 16 Apr 2003 22:28:24 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: regexp. for a two dates delimited with a minus
Message-Id: <b7keqv$1t8p6$1@ID-184292.news.dfncis.de>
Fizgig wrote:
> I need a regexp for a string with this format
>
> DD.MM.YYYY-DD.MM.YYYY
>
> I have already a great working expression for a single date;
<snip>
> How can i get it working for two dates delimited with a minus?
If you don't want to drop that 'terrible' regexp, you can of course do
something like:
sub datecheck {
my @parts = split /-/, shift;
my $df =
'(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})';
return ($parts[0] =~ /$df/ and $parts[1] =~ /$df/) ? 0 : 1;
}
datecheck($string) returns 0 if okay, or else 1.
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 16 Apr 2003 13:25:20 -0500
From: kaeli <infinite.possibilities@NOSPAMatt.net>
Subject: Re: Remembering previous values of the form.
Message-Id: <MPG.19075c0b3d9a41dc989b95@nntp.lucent.com>
And the fire said to me...on Wed, 16 Apr 2003 17:58:26 +0000 (UTC) in
article <b7k5k2$9of$1@flood.xnet.com>, shah@typhoon.xnet.com said:
>
> Folks,
>
> I am writing timesheet entry system using perl CGI.pm and Javascript,
> on Linux.
>
> I have completed the form that displays time already entered, and allows
> the user to add/chage/delete time. My problem is trying to remember what
> the timesheet looked like when it was displayed, so that I can somehow
> compare it with the new values when user presses "Save" button.
>
Why?
Depending on the answer to that question, it may be better for you to do
this server-side with the database and PL/SQL (or equivalent) trigger.
DB procedures run much faster than client-side code and don't depend on
the user having script enabled. So if it's a history type thing you are
looking for, use the DB.
If for some reason you want it to be done in javascript, have the Perl
write the old values to a javascript array. When the user presses save,
loop through the form values and compare to array values. You can make
this even easier by using an associative array with names the same as
your form element names. ie myArray[myElement.name] will refer to the
array (old) value for the corresponding element (myElement.value,
generally speaking). No hidden elements needed.
If you need more detail, let me know.
----------------------------------------------------
~kaeli~
Never argue with an idiot! People may not be able to tell you apart.
Contrary to popular opinion, the plural of 'anecdote' is not 'fact'.
http://www.ipwebdesign.net/wildAtHeart/
http://www.ipwebdesign.net/kaelisSpace/
----------------------------------------------------
------------------------------
Date: Wed, 16 Apr 2003 22:54:24 GMT
From: Eric Wilhelm <ericw@nospam.ku.edu>
Subject: Re: Remote Module Installation
Message-Id: <pan.2003.04.16.16.54.38.189443.1626@nospam.ku.edu>
On Wed, 16 Apr 2003 12:58:28 -0500, Vijoy Varghese wrote:
>> It looks like it is a strictly-perl module, so you don't need to worry
>> about compiling. Just get the .pm file onto the server and find a way
>> to use pathtomodule.pm.
>
> I added this line
> use lib "/home/thediffe/public_html/project/modules";
I assume that you followed this with
use Digest::HMAC_MD5;
as shown in your original post?
>and put -HMAC.pm
> -HMAC_MD5.pm
> -HMAC_SHA1.pm
> -MD2.pm
> -MD4.pm
> -MD5.pm
> -SHA1.pm
>
do those names really have dashes?
If so, that would be your problem. See, the :: is really just a "/" that
you don't have to quote. So the "use" line is asking for the filename by
saying get <some folder in @INC>/<subfolder>/<modulename>.pm
> files under /public_html/project/modules/Digest folder
>
> and i am getting this error... :-(
>
> Wed Apr 16 13:50:43 2003] [error] [client 202.88.224.211] Premature end
> of script headers: /home/thediffe/public_html/project/temp.cgi BEGIN
> failed--compilation aborted at
> /home/thediffe/public_html/project/modules/Digest/HMAC_MD5.pm line 5.
>
> first 10 lines of HMAC_MD5.pm is
> package Digest::HMAC_MD5;
1
> $VERSION="1.01";
2
>
3
> use strict;
4
> use Digest::MD5 qw(md5);
5
The module is asking for one of its buddies, no? I think it must be
related to the filenames (if they really have the dashes you show above).
If not that, I think it may be the use strict and have something to do
with how you have the modules installed, but I am never so strict, so I'm
out of my experience here.
--Eric
------------------------------
Date: 16 Apr 2003 18:53:34 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Stumped!
Message-Id: <u9vfxe8h29.fsf@wcl-l.bham.ac.uk>
Daniel Lautenschleger <delautenschl@[nospam]wisc.edu> rudely vomits
TOFU in our faces:
[ rudeness corrected ]
> Steven Kuo wrote:
> > On Thu, 3 Apr 2003, pooba53 wrote:
> >
> >>I have a hash that has multiple values associated with a single key.
> >>i.e.:
> >>
> >>key is "CG2349"
> >>
> >>values associate with "CG2349" are :
> >>
> >>(1,2,3,4,5)
> >>(2,3,4,5,6,7)
> >>(10,11,12,13)
> >>
> >>Since I have multiple values associated with a single key, I'm using
> >>references.
> > So I imagine that you have a hash structure similar to this:
> > my %hash = (
> > CG2349 => [ [1,2,3,4,5], [2,3,4,5,6,7], [10,11,12,13] ],
> > );
> >
> >>I'm trying to get each value (the three above) into a single array so I
> >>can extract unique elements from the "merge".
Why do you want a single _array_? The natural Perl datatype to
represent a set is a hash (with no values).
> >>
> >>The problem I'm having is how to do this and create a new hash with one
> >>value associated with each key after merging and "uniquing".
> >>
> >>I'm pretty sure I need to use the each function such as:
> >>
> >>while (($key, $value) = each(${@MyArray}) {
> >>
> > each expects a hash, not a scalar.
> > You may want to try something like this:
> > #! /usr/local/bin/perl
> > use strict;
> > use warnings;
> > my %hash = (
> > CG2349 => [ [1,2,3,4,5], [2,3,4,5,6,7], [10,11,12,13] ],
> > );
> > while (my ($key,$aref) = each %hash) {
> > my %seen;
> > my @unique = grep !$seen{$_}++, map @$_, @$aref;
> > print "unique elements for $key : @unique\n";
> > }
> > See also
> > % perldoc -q 'remove duplicate elements'
> >
> I should have been a little clearer.
You _still_ need to be clearer.
> I've already got unique elements for each key. There are no duplicates.
>
> I need to get rid of each "subset" item. I need unique "items" only.
> For instance:
>
> If the following elements are:
> (1,2,3,4,5)
> (2,3,4,5,6,7)
> (10,11,12,13)
>
> The unique items from each element would be
>
> 1,2,3,4,5,6,7,10,11,12,13
And how is this requirment different from what Steven showed you?
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 16 Apr 2003 13:39:17 -0500
From: pooba53 <delautenschl@[nospam]wisc.edu>
Subject: Re: Stumped!
Message-Id: <b7k80j$7as$1@news.doit.wisc.edu>
Okay, I tried the code and the unique values keep printing as nothing
(i.e. "").
I wonder if there's something that needs to be modified to accommodate
the refences being used.
-Dan
Brian McCauley wrote:
> Daniel Lautenschleger <delautenschl@[nospam]wisc.edu> rudely vomits
> TOFU in our faces:
>
> [ rudeness corrected ]
>
>
>>Steven Kuo wrote:
>>
>>>On Thu, 3 Apr 2003, pooba53 wrote:
>>>
>>>
>>>>I have a hash that has multiple values associated with a single key.
>>>>i.e.:
>>>>
>>>>key is "CG2349"
>>>>
>>>>values associate with "CG2349" are :
>>>>
>>>>(1,2,3,4,5)
>>>>(2,3,4,5,6,7)
>>>>(10,11,12,13)
>>>>
>>>>Since I have multiple values associated with a single key, I'm using
>>>>references.
>>>
>>>So I imagine that you have a hash structure similar to this:
>>>my %hash = (
>>> CG2349 => [ [1,2,3,4,5], [2,3,4,5,6,7], [10,11,12,13] ],
>>>);
>>>
>>>
>>>>I'm trying to get each value (the three above) into a single array so I
>>>>can extract unique elements from the "merge".
>
>
> Why do you want a single _array_? The natural Perl datatype to
> represent a set is a hash (with no values).
>
>
>>>>The problem I'm having is how to do this and create a new hash with one
>>>>value associated with each key after merging and "uniquing".
>>>>
>>>>I'm pretty sure I need to use the each function such as:
>>>>
>>>>while (($key, $value) = each(${@MyArray}) {
>>>>
>>>
>>>each expects a hash, not a scalar.
>>>You may want to try something like this:
>>>#! /usr/local/bin/perl
>>>use strict;
>>>use warnings;
>>>my %hash = (
>>> CG2349 => [ [1,2,3,4,5], [2,3,4,5,6,7], [10,11,12,13] ],
>>>);
>>>while (my ($key,$aref) = each %hash) {
>>> my %seen;
>>> my @unique = grep !$seen{$_}++, map @$_, @$aref;
>>> print "unique elements for $key : @unique\n";
>>>}
>>>See also
>>>% perldoc -q 'remove duplicate elements'
>>>
>
>
>>I should have been a little clearer.
>
>
> You _still_ need to be clearer.
>
>
>>I've already got unique elements for each key. There are no duplicates.
>>
>>I need to get rid of each "subset" item. I need unique "items" only.
>
>
>
>>For instance:
>>
>>If the following elements are:
>>(1,2,3,4,5)
>>(2,3,4,5,6,7)
>>(10,11,12,13)
>>
>>The unique items from each element would be
>>
>>1,2,3,4,5,6,7,10,11,12,13
>
>
> And how is this requirment different from what Steven showed you?
>
------------------------------
Date: Wed, 16 Apr 2003 14:01:36 -0500
From: pooba53 <delautenschl@[nospam]wisc.edu>
Subject: Re: Stumped!
Message-Id: <b7k9ae$84n$1@news.doit.wisc.edu>
With the following code:
while (($key,$aref) = each (%AllGenes)) {
@unique = grep !$seen2{$_}++, map $_, $aref;
print "Unique elements for $key is: @{$unique[0]}\n\n";
}
I do get something to print out, but I cannot figure out how to print
all of the @unique list instead of the first element.
Thanks.
-Dan
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 4855
***************************************