|
|
Up |
  |
Author: PerlFAQ ServerPerlFAQ Server
Date: May 16, 2008 18:03
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
4.54: Why does defined() return true on empty arrays and hashes?
The short story is that you should probably only use defined on scalars
or functions, not on aggregates (arrays and hashes). See "defined" in
perlfunc in the 5.004 release or later of Perl for more detail.
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
|
| Show full article (1.25Kb) |
|
| |
no comments
|
|
  |
Author: PerlFAQ ServerPerlFAQ Server
Date: May 16, 2008 12:03
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
4.29: How can I count the number of occurrences of a substring within a string?
There are a number of ways, with varying efficiency. If you want a count
of a certain single character (X) within a string, you can use the
"tr///" function like so:
$string = "ThisXlineXhasXsomeXx'sXinXit";
$count = ($string =~ tr/X//);
print "There are $count X characters in the string";
This is fine if you are just looking for a single character. However, if
you are trying to count multiple character substrings within a larger
string, "tr///" won't work. What you can do is wrap a while() loop
around a global pattern match. For example, let's count negative
integers:
|
| Show full article (2.06Kb) |
|
| |
no comments
|
|
  |
Author: Bernie CosellBernie Cosell
Date: May 16, 2008 10:37
I'm trying to parse an email address and I can't seem to get Email::Address
to work quite.
#!/usr/bin/perl
use strict;
use warnings ;
use Email::Address ;
my $addr = "My Name verizon.net>\n" ;
my @addrs = Email::Address::parse($addr) ;
warn scalar(@addrs) ;
Gets me "0" -- it appears not to parse that string, which certainly looks
like a legal email addr to me [am I missing some problem with it?? -- I
actually pulled it out of a file of addresses that sendmail is happily
sending-via]. What am I missing here? THANKS!
/bernie\
--
Bernie Cosell Fantasy Farm Fibers
bernie@ fantasyfarm.com Pearisburg, VA
--> Too many people, too few sheep <--
|
| |
|
4 Comments |
|
  |
Author: PerlFAQ ServerPerlFAQ Server
Date: May 16, 2008 06:03
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
4.50: How do I select a random element from an array?
Use the "rand()" function (see "rand" in perlfunc):
$index = rand @array;
$element = $array[$index];
Or, simply:
my $element = $array[ rand @array ];
--------------------------------------------------------------------
|
| Show full article (1.23Kb) |
|
no comments
|
|
  |
Author: KeenlearnerKeenlearner
Date: May 16, 2008 03:41
Hello, I am using Wordnet::QueryData which allow access to a very huge
dictionary data. The initialization of object
my $wn = WordNet::QueryData->new;
took
2 wallclock secs ( 2.36 usr + 0.07 sys = 2.43 CPU)
Then the subsequent request for the data is exetremely fast
For the lines below took
0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
print "Synset: ", join(", ", $wn->querySense("cat#n#7", "syns")),
"\n";
print "Hyponyms: ", join(", ", $wn->querySense("cat#n#1", "hypo")),
"\n";
print "Parts of Speech: ", join(", ", $wn->querySense("run")), "\n";
print "Senses: ", join(", ", $wn->querySense("run#v")), "\n";
print "Forms: ", join(", ", $wn->validForms("lay down#v")), "\n";
print "Noun count: ", scalar($wn->listAllWords("noun")), "\n";
print "Antonyms: ", join(", ", $wn->queryWord("dark#n#1", "ants")),
"\n";
|
| Show full article (1.19Kb) |
|
2 Comments |
|
  |
Author: ElaEla
Date: May 16, 2008 02:45
The following codes are not accepted by Perl and even string concatenation
doesn't work. Any suggestions?
#!/usr/bin/perl
$infile = $ARGV[0];
foreach $k (0..10) {
$outfile = $infile . $k;
open (OFP$k, ">$outfile");
}
$i = $size/10000;
print OFP$i "something"; #0-10k, 10-20k, .... 90-100k...
|
| |
|
7 Comments |
|
  |
Author: PerlFAQ ServerPerlFAQ Server
Date: May 16, 2008 00:03
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
4.52: How do I sort an array by (anything)?
Supply a comparison function to sort() (described in "sort" in
perlfunc):
@list = sort { $a <=> $b } @list;
The default sort function is cmp, string comparison, which would sort
"(1, 2, 10)" into "(1, 10, 2)". "<=>", used above, is the numerical
comparison operator.
If you have a complicated function needed to pull out the part you want
to sort on, then don't do it inside the sort function. Pull it out
first, because the sort BLOCK can be called many times for the same
element. Here's an example of how to pull out the first word after the
first number on each item, and then sort those words case-insensitively.
|
| Show full article (2.80Kb) |
|
no comments
|
|
  |
Author: tadmctadmc
Date: May 15, 2008 23:11
Outline
Before posting to comp.lang.perl.misc
Must
- Check the Perl Frequently Asked Questions (FAQ)
- Check the other standard Perl docs (*.pod)
Really Really Should
- Lurk for a while before posting
- Search a Usenet archive
If You Like
- Check Other Resources
Posting to comp.lang.perl.misc
Is there a better place to ask your question?
- Question should be about Perl, not about the application area
How to participate (post) in the clpmisc community
- Carefully choose the contents of your Subject header
- Use an effective followup style
- Speak Perl rather than English, when possible
- Ask perl to help you
- Do not re-type Perl code
- Provide enough information ...
|
| Show full article (16.63Kb) |
|
no comments
|
|
  |
Author: PerlFAQ ServerPerlFAQ Server
Date: May 15, 2008 18:03
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
4.35: How do I find the soundex value of a string?
(contributed by brian d foy)
You can use the Text::Soundex module. If you want to do fuzzy or close
matching, you might also try the "String::Approx", and
"Text::Metaphone", and "Text::DoubleMetaphone" modules.
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
|
| Show full article (1.25Kb) |
|
no comments
|
|
  |
|
  |
Author: kroneckerkronecker
Date: May 15, 2008 17:38
I create a text file on the server remote.txt ok with the following
code (the last part of it)
$first_name = $FORM{command1};
$last_name = $FORM{command2};
open (example, ">remote.txt") || die ("Could not open file. $!");
print example "$first_name\n$last_name\n";
close (example);
print "Content-type:text/html\r\n\r\n";
This bit causes errors...... chmod
0777,'remote.txt'; ......................................
print "";
print "";
print " Processed";
print "";
print "";
print "Commands $first_name $last_name - Sent to text file";
print "";
print "";
|
| Show full article (0.91Kb) |
|
2 Comments |
|
|
|
|