[23732] in Perl-Users-Digest
Perl-Users Digest, Issue: 5938 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 14 18:06:03 2003
Date: Sun, 14 Dec 2003 15:05:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 14 Dec 2003 Volume: 10 Number: 5938
Today's topics:
Re: [newbie] simple substitution/matching <geoffroy.braem@skynet.be>
ActiveState <david@digiportal.com>
Behave Differently According to Time (Hour) <test@test.com>
Re: Behave Differently According to Time (Hour) <asu1@c-o-r-n-e-l-l.edu>
Re: Behave Differently According to Time (Hour) <krahnj@acm.org>
Re: Behave Differently According to Time (Hour) <tony_curtis32@_SPAMTRAP_yahoo.com>
Re: Behave Differently According to Time (Hour) <jurgenex@hotmail.com>
Re: Behave Differently According to Time (Hour) <abigail@abigail.nl>
Re: Behave Differently According to Time (Hour) <jwillmore@remove.adelphia.net>
Re: Caching results (?) of lengthy cgi process (Randal L. Schwartz)
Re: Caching results (?) of lengthy cgi process <pkent77tea@yahoo.com.tea>
Re: Caching results (?) of lengthy cgi process <REMOVEsdnCAPS@comcast.net>
Re: Calling Another Script <geoffroy.braem@skynet.be>
Re: Calling Another Script (Tad McClellan)
Re: Comparison Value <pkent77tea@yahoo.com.tea>
Re: Concatenation Question <geoffroy.braem@skynet.be>
Re: Concatenation Question <REMOVEsdnCAPS@comcast.net>
Re: Concatenation Question <jwillmore@remove.adelphia.net>
encrypt email address to a string <test@test.com>
Re: encrypt email address to a string <REMOVEsdnCAPS@comcast.net>
Re: encrypt email address to a string <jwillmore@remove.adelphia.net>
Re: how to read data from EXCEL <test@test.com>
Re: How to write MIME header for application/x-javascri (Bart Van der Donck)
Re: In search of elegant code - searching keys of hashe <geoffroy.braem@skynet.be>
Re: In search of elegant code - searching keys of hashe (Tad McClellan)
Re: last index of @array <geoffroy.braem@skynet.be>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 14 Dec 2003 11:30:47 -0500
From: geoffroy <geoffroy.braem@skynet.be>
Subject: Re: [newbie] simple substitution/matching
Message-Id: <3fdc3bcd$0$2879$ba620e4c@reader1.news.skynet.be>
Sean Don wrote:
> Hi,
>
> I'm learning perl and trying to write a simple VCARD to Mutt alias
> converter. I feel a little dumb having to ask, but I can't figure it
> out and it's getting frustrating. I looked up some perl examples
> including a perl script that is supposed to do this, but it didn't work
> quite right.
>
>
> I have this:
>
> $_ = "N:Lastname;Firstname;M.";
>
> s/^N://go;
> s/.*;//go;
>
> print $_;
>
>
> It outputs this:
>
> M.
>
>
> However, I want it to output this:
>
> Firstname
>
>
> Thank You In Advance,
> ~ Sean ~
>
This is due to the greedy matching. The regex matches as much as possible.
What you need is
> $_ = "N:Lastname;Firstname;M.";
/N:\w+;(\w+);M./;
print $1;
or
s/N:\w+;(\w+);M./$1/;
print $_;
Geoffroy
------------------------------
Date: Sun, 14 Dec 2003 20:21:46 GMT
From: "David Jameson" <david@digiportal.com>
Subject: ActiveState
Message-Id: <uD3Db.146187$655.26195156@news4.srv.hcvlny.cv.net>
Does anyone know how to make the latest ActiveState PDK support
PerlScript.COM?
Thanks,
David Jameson
------------------------------
Date: Sun, 14 Dec 2003 04:30:15 GMT
From: "Fengshui" <test@test.com>
Subject: Behave Differently According to Time (Hour)
Message-Id: <rHRCb.451813$0v4.20914673@bgtnsc04-news.ops.worldnet.att.net>
Is there an easy way to know what hour I am in from the output of time()?
1 line calculation to know if it is day time or night time (7pm-6am ETS)
I think there must be way just like this
(time + factor) %something here = bahbahbah.. then from bahbahbah, I can
tell what hour is now.
Thanks.
------------------------------
Date: 14 Dec 2003 05:15:10 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: Behave Differently According to Time (Hour)
Message-Id: <Xns94512926AB0Dasu1cornelledu@132.236.56.8>
"Fengshui" <test@test.com> wrote in news:rHRCb.451813$0v4.20914673
@bgtnsc04-news.ops.worldnet.att.net:
> Is there an easy way to know what hour I am in from the output of time()?
>
> 1 line calculation to know if it is day time or night time (7pm-6am ETS)
>
> I think there must be way just like this
> (time + factor) %something here = bahbahbah.. then from bahbahbah, I can
> tell what hour is now.
perldoc -f time
perldoc -f localtime
perldoc -q gmtime
What part is not clear?
--
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov
------------------------------
Date: Sun, 14 Dec 2003 05:17:45 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Behave Differently According to Time (Hour)
Message-Id: <3FDBF26B.AA7F0860@acm.org>
Fengshui wrote:
>
> Is there an easy way to know what hour I am in from the output of time()?
>
> 1 line calculation to know if it is day time or night time (7pm-6am ETS)
Maybe, but it would be easier to use the output from localtime().
perldoc -f localtime
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 13 Dec 2003 23:18:15 -0600
From: Tony Curtis <tony_curtis32@_SPAMTRAP_yahoo.com>
Subject: Re: Behave Differently According to Time (Hour)
Message-Id: <871xr86kq0.fsf@limey.hpcc.uh.edu>
>> On Sun, 14 Dec 2003 04:30:15 GMT,
>> "Fengshui" <test@test.com> said:
> Is there an easy way to know what hour I am in from the
> output of time()? 1 line calculation to know if it is day
> time or night time (7pm-6am ETS)
perldoc -f localtime
hth
t
------------------------------
Date: Sun, 14 Dec 2003 08:22:56 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Behave Differently According to Time (Hour)
Message-Id: <A5VCb.11035$gk1.3731@nwrddc01.gnilink.net>
Fengshui wrote:
> Is there an easy way to know what hour I am in from the output of
> time()?
Certainly, if you insist on doing it the hard way.
time() returns the seconds since 00:00:00 UTC, January 1, 1970 (at least on
Unix systems).
Just divide them by 3600 and you get the hours since the epoch. Divide those
hours modulo 24 and you get the hour in the day in UTC. Adjust for the local
time zone and your are done.
However, why aren't you simply using localtime()?
jue
------------------------------
Date: 14 Dec 2003 11:04:11 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Behave Differently According to Time (Hour)
Message-Id: <slrnbtogtb.les.abigail@alexandra.abigail.nl>
Fengshui (test@test.com) wrote on MMMDCCLVII September MCMXCIII in
<URL:news:rHRCb.451813$0v4.20914673@bgtnsc04-news.ops.worldnet.att.net>:
() Is there an easy way to know what hour I am in from the output of time()?
perldoc -f localtime
Abigail
--
srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
//=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"
------------------------------
Date: Sun, 14 Dec 2003 16:42:07 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: Behave Differently According to Time (Hour)
Message-Id: <20031214114207.2727cd79.jwillmore@remove.adelphia.net>
On Sun, 14 Dec 2003 04:30:15 GMT
"Fengshui" <test@test.com> wrote:
> Is there an easy way to know what hour I am in from the output of
> time()?
>
> 1 line calculation to know if it is day time or night time (7pm-6am
> ETS)
>
> I think there must be way just like this
> (time + factor) %something here = bahbahbah.. then from bahbahbah, I
> can tell what hour is now.
You could look over the Date::Calc or Date::Manip modules as well as
the other posts made.
With the 2 modules I mentioned, you can not only find out what time it
is now, but perform time calculations as well.
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
Johnson's First Law: When any mechanical contrivance fails, it
will do so at the most inconvenient possible time.
------------------------------
Date: Sun, 14 Dec 2003 04:56:20 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Caching results (?) of lengthy cgi process
Message-Id: <c745137a42619932140433ef144416b8@news.teranews.com>
>>>>> "Henry" == Henry <henryn@zzzspacebbs.com> writes:
Henry> In most basic terms this means having the
Henry> time-since-last-update visible in a base-page prompt: "It's
Henry> been 'n' days since the last update, hit 'submit' to re-synch."
Henry> That is a bit different from an automagic, crontab-scheduled
Henry> update.
Consider the code at <http://www.stonehenge.com/merlyn/LinuxMag/col20.html>.
And why didn't you ask this in a CGI group instead of a Perl group?
print "Just another Perl hacker,"
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Sun, 14 Dec 2003 15:32:49 +0000
From: pkent <pkent77tea@yahoo.com.tea>
Subject: Re: Caching results (?) of lengthy cgi process
Message-Id: <pkent77tea-E0BDB9.15324614122003@ptb-nnrpp01.plus.net>
In article <BC00ACF7.1943B%henryn@zzzspacebbs.com>,
Henry <henryn@zzzspacebbs.com> wrote:
> Consider: Perl "results.cgi" generates a perfectly good page of juicy
> results, except it takes much too long in human time to do it, over 30
> seconds -- and it is likely to get longer, not shorter.
>
> In fact, the results don't change very often, so the hard work only needs to
> be done periodically, maybe every couple of weeks. So, most of the time,
> there's no need to have the user wait for a full update.
If I understand your situation correctly, what I'd do is:
1) run your perl program to generate foo.html
2) don't point people to results.cgi, point them to foo.html
3) periodically run your perl program to generate an updated foo.html
(you might do this as a cron job, or on an ad-hoc basis)
Another option is to write to disk the _results_ of the query, not the
HTML that is finally sent to the browser. You can then have a relatively
lightweight CGI that reads in the results from disk and presents them to
the user in the way that the user wants. This might be useful where you
have a large number of results and users can choose "10 results per
page" or "give me page 4 of the results".
In parallel you might also want to find out why the query is taking so
long, if only for future reference when you want (or are asked) to have
another query where the results _do_ change.
P
--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
------------------------------
Date: Sun, 14 Dec 2003 10:35:08 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Caching results (?) of lengthy cgi process
Message-Id: <Xns945175C7752B2sdn.comcast@216.196.97.136>
merlyn@stonehenge.com (Randal L. Schwartz) wrote in
news:c745137a42619932140433ef144416b8@news.teranews.com:
> And why didn't you ask this in a CGI group instead of a Perl group?
Because Perl and CGI are the same thing, of course.... right?
--
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
------------------------------
Date: Sun, 14 Dec 2003 11:38:12 -0500
From: geoffroy <geoffroy.braem@skynet.be>
Subject: Re: Calling Another Script
Message-Id: <3fdc3d8a$0$2879$ba620e4c@reader1.news.skynet.be>
Matt wrote:
> I need to have perl script call another script on a Linux box and parse its
> output. When I call this other script it returns between 1 and 100 lines of
> data depending on time of day. So I need to get this data that script
> returns into my script. At the moment all I really need is a count of how
> many lines of data actually. So how would I do that?
>
> Matt
>
>
>
>
$nr_of_lines = map{chomp; $_} `your_script`;
A list in scalar context return the number of elements in that list.
Geoffroy
------------------------------
Date: Sun, 14 Dec 2003 10:10:08 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Calling Another Script
Message-Id: <slrnbtp2r0.a2s.tadmc@magna.augustmail.com>
geoffroy <geoffroy.braem@skynet.be> wrote:
> $nr_of_lines = map{chomp; $_} `your_script`;
Why is the chomp() needed when we only want the count anyway?
> A list in scalar context return the number of elements in that list.
No it doesn't.
First, "there's no such thing as a list in scalar context". [1]
Second, the only list in your code is the one returned by backticks,
and it is in list context.
It is the documented behavior of map() that it return the
number of elements when in scalar context, so:
A map() in scalar context return the number of elements in that list.
[1] perldoc -q "list and an array"
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 14 Dec 2003 15:50:51 +0000
From: pkent <pkent77tea@yahoo.com.tea>
Subject: Re: Comparison Value
Message-Id: <pkent77tea-8E497D.15505114122003@ptb-nnrpp01.plus.net>
In article <42f55bd6.0312121326.323c5ce6@posting.google.com>,
cool_ian10@hotmail.com (ThERiZla) wrote:
> I want find in an array a value start by this character : "-".
> This is two examples of value in my array, I want find: -c, -s .
> I use a while to scan my array, but I don't know how compare my values to find
> the good one.
>
> Suggestions and/or examples anyone ?
my @foo = qw(-c cheap as -s chips);
foreach (@foo) {
if (index($_, '-') == 0) {
print "Found the item $_\n";
# or do whatever else you want...
}
}
__END__
That prints:
Found the item -c
Found the item -s
on my machine.
[ But as pointed out by another poster, if you're parsing command line
options you should use Getopt::Std or Getopt::Long unless you have a
_really_ good reason not to (e.g. you're trying to implement a drop-in
replacement for something with unusual option syntax, such as tar(1) or
the much more unusual usfdump(1M)) ]
P
--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
------------------------------
Date: Sun, 14 Dec 2003 11:43:48 -0500
From: geoffroy <geoffroy.braem@skynet.be>
Subject: Re: Concatenation Question
Message-Id: <3fdc3ed9$0$2879$ba620e4c@reader1.news.skynet.be>
Chip wrote:
> How can I add a ".jpg" extension to a string?
>
> I'm trying the following with 12345 as the input.
>
> #!/usr/bin/perl -w
> print "Please enter the item picture number to remove: ";
> my $number=<STDIN>;
> my $path=http://someplace.com/images/;
> my $ext=".jpg";
> my $picture="$path . $number . $ext";
> system rm $picture;
> print "$picture has been removed";
> __END__
>
> The output I'm hoping for is:
> http://someplace.com/images/12345.jpg has been removed
>
> What I get is:
> http://someplace.com/images/12345 has been removed
>
> How do I add the ".jpg"
>
> Thanks
> Chip
>
>
The problem you are having is that your input ($number) has a new line
character.
You should use
chomp($number = <STDIN>);
instead.
Geoffroy
------------------------------
Date: Sun, 14 Dec 2003 11:10:22 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Concatenation Question
Message-Id: <Xns94517BC074DAAsdn.comcast@216.196.97.136>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
geoffroy <geoffroy.braem@skynet.be> wrote in
news:3fdc3ed9$0$2879$ba620e4c@reader1.news.skynet.be:
> The problem you are having is that your input ($number) has a new
line
> character.
Really? How did you divine that from:
> Chip wrote:
>> The output I'm hoping for is:
>> http://someplace.com/images/12345.jpg has been removed
>>
>> What I get is:
>> http://someplace.com/images/12345 has been removed
>>
>> How do I add the ".jpg"
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
iD8DBQE/3JlfY96i4h5M0egRAtscAJ0cgeETZZ3azGoDpeay4cwZO/IlvQCeJ68T
ALygYWgFNpY8qnWFUJm/MkE=
=ujac
-----END PGP SIGNATURE-----
------------------------------
Date: Sun, 14 Dec 2003 19:34:15 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: Concatenation Question
Message-Id: <20031214143414.42f76278.jwillmore@remove.adelphia.net>
On Sun, 14 Dec 2003 11:43:48 -0500
geoffroy <geoffroy.braem@skynet.be> wrote:
> Chip wrote:
> > How can I add a ".jpg" extension to a string?
> >
> > I'm trying the following with 12345 as the input.
> >
> > #!/usr/bin/perl -w
> > print "Please enter the item picture number to remove: ";
> > my $number=<STDIN>;
> > my $path=http://someplace.com/images/;
> > my $ext=".jpg";
> > my $picture="$path . $number . $ext";
> > system rm $picture;
> > print "$picture has been removed";
> > __END__
<snip>
> The problem you are having is that your input ($number) has a new
> line character.
>
> You should use
>
>
> chomp($number = <STDIN>);
>
> instead.
I'd write something like ... (untested)
#!/usr/bin/perl -w
use strict;
use diagnostics;
print "Please enter the item picture number to remove: ";
chomp( my $number = <STDIN> );
my $path = 'http://someplace.com/images/';
my $ext = '.jpg';
my $picture = sprintf "%s%s%s", $path, $number, $ext;
unlink $picture or die "Can't remove $picture: $!\n";
print "$picture has been removed";
*How* the OP's code ran is beyond me. I got a syntax error right out
of the gate.
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
When Marriage is Outlawed, Only Outlaws will have Inlaws.
------------------------------
Date: Sun, 14 Dec 2003 18:26:09 GMT
From: "Fengshui" <test@test.com>
Subject: encrypt email address to a string
Message-Id: <5X1Db.455457$0v4.20982854@bgtnsc04-news.ops.worldnet.att.net>
domain rule is as: caseless letters, numbers and -
username rule is letters. numbers, -, _ and .
so if I want to covert/encript an email into a letter/number only string,
how do i do this without adding length to the string? i don't want to just
exchange ascii code for each one.
------------------------------
Date: Sun, 14 Dec 2003 13:17:57 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: encrypt email address to a string
Message-Id: <Xns94519161979Csdn.comcast@216.196.97.136>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
"Fengshui" <test@test.com> wrote in
news:5X1Db.455457$0v4.20982854@bgtnsc04-news.ops.worldnet.att.net:
> domain rule is as: caseless letters, numbers and -
>
> username rule is letters. numbers, -, _ and .
>
> so if I want to covert/encript an email into a letter/number only
> string, how do i do this without adding length to the string? i
don't
> want to just exchange ascii code for each one.
How strong do you want the encryption to be? Unless you have an
extreeeemely simple scheme, you're going to add length. Plus, you're
going to have to do annoying things like Huffman-encode bit
sequences. Ick.
What's this for, anyhow?
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
iD8DBQE/3LdGY96i4h5M0egRAl/WAJ4lsRfjIm1ju7ETClf+RRDvgHmRjQCgu6XQ
STR6NZgj7WAaDFU2GPAWtD8=
=+Afu
-----END PGP SIGNATURE-----
------------------------------
Date: Sun, 14 Dec 2003 19:38:32 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: encrypt email address to a string
Message-Id: <20031214143832.16c26967.jwillmore@remove.adelphia.net>
On Sun, 14 Dec 2003 18:26:09 GMT
"Fengshui" <test@test.com> wrote:
> domain rule is as: caseless letters, numbers and -
>
> username rule is letters. numbers, -, _ and .
>
> so if I want to covert/encript an email into a letter/number only
> string, how do i do this without adding length to the string? i
> don't want to just exchange ascii code for each one.
I have no idea what you're trying to accomplish other than you want to
encrypt something.
Try one of the many Crypt modules available.
Recently, someone released a module to obfuscate email addresses -
maybe that's really what you want to do?
http://search.cpan.org
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
The Kennedy Constant: Don't get mad -- get even.
------------------------------
Date: Sun, 14 Dec 2003 04:31:23 GMT
From: "Fengshui" <test@test.com>
Subject: Re: how to read data from EXCEL
Message-Id: <vIRCb.451818$0v4.20914757@bgtnsc04-news.ops.worldnet.att.net>
I would like to know if excel file is a BINARY or TEXT file. I konw .csv is
text file.
> I need to read data from EXCEL worksheets in this way:
> The script perl asks me to write a number .
> I type a Number f.e. 1000 , the script should look for the number 1000
> inside a
> excel worksheet file and if it find it, should capture all the other
> numbers reported in the raw where there is the number 1000.
> the mother number (1000) is always in the first colomn of excel sheet.
>
> col1 col2 col3 col4 col5
> raw1 900 002 004 006
> raw2 1000 345 445 888 777
>
> once the script had found the wanted number (1000) in the raw2 col1 ,
> it gives me the other 4 number (345 445 888 777).
> and put them in an array.
> the cells with number in the excel workspread are variable from 0 to ...,
> the script should be able to read all the cells of the raw till the first
> empty cell.
> there is someone could help me
> Thanks Carlo
>
>
>
>
------------------------------
Date: 14 Dec 2003 11:46:57 -0800
From: bart@nijlen.com (Bart Van der Donck)
Subject: Re: How to write MIME header for application/x-javascript?
Message-Id: <b5884818.0312141146.7165693c@posting.google.com>
> Anyone done this before?
Yes, with Excel files.
I don't think it's such a good idea to generate your javascript files
separately. My provider uses heavy buffering on them.
The previous poster typed the code that you need. Personally, I would
code the javascript inside your main document. But maybe your .js file
is used by a lot of documents.
To avoid caching, you might use someting like:
<script language="javascript"
src="http://www.domain.com/javascript.js?RANDOMHERE"></script>
You'll need a server process to generate the random value. But in case
of a server process, I wonder why you wouldn't let the main document
generate the dynamic js code. Maybe you could let javascript generate
a random value in main file, and use that value to call your external
javascript to make sure you're not caching. I don't know if browsers
will get away with that, because then you start using <script> inside
<script> tags.
Bart
------------------------------
Date: Sun, 14 Dec 2003 11:47:45 -0500
From: geoffroy <geoffroy.braem@skynet.be>
Subject: Re: In search of elegant code - searching keys of hashes in array
Message-Id: <3fdc3fc7$0$292$ba620e4c@reader5.news.skynet.be>
David Filmer wrote:
> Suppose I have an array of hashes of names and social-security
> numbers:
>
> @ARRAY = (
> { first => 'John', last => 'Doe', ssn => '123-45-6789' },
> { first => 'Fred', last => 'Ree', ssn => '9876-54-321' }
> );
>
> Now suppose I want to print an error if any particular SSNs (say
> '234-56-7890' and '0987-65-4321') are not found in my @ARRAY.
> Obviously I can't just grep the array. I could build an intermediate
> hash (%SSN) like this:
>
> for (@ARRAY) { $SSN{$$_{'ssn'}}++ }
> for (qw/234-56-7890 0987-65-4321/) {
> print "Not found: $_\n" unless $SSN{$_}
> }
>
> but that's REALLY ugly (I don't like the creation of the intermediate
> %SSN hash). I would appreciate suggestions for a more elegant
> approach.
You can use this sub. it takes one parameter, the ssn.
sub does_ssn_exist{
foreach $hashref (@ARRAY){
if($hashref->{'ssn'} eq $_[0]){retrun 1}
}
return 0;
}
Geoffroy
------------------------------
Date: Sun, 14 Dec 2003 10:12:35 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: In search of elegant code - searching keys of hashes in array
Message-Id: <slrnbtp2vj.a2s.tadmc@magna.augustmail.com>
geoffroy <geoffroy.braem@skynet.be> wrote:
> You can use this sub.
I doubt that.
> if($hashref->{'ssn'} eq $_[0]){retrun 1}
^^^^^^
What does the retrun() function do?
Have you seen the Posting Guidelines that are posted here frequently?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 14 Dec 2003 11:38:52 -0500
From: geoffroy <geoffroy.braem@skynet.be>
Subject: Re: last index of @array
Message-Id: <3fdc3db1$0$2879$ba620e4c@reader1.news.skynet.be>
Martin wrote:
> Hi folks!
>
> is there a special variable which holds the size or last index of any
> @array? some like the opposite of $[ ...
>
> cu, md
$#array
Geoffroy
------------------------------
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 5938
***************************************