📄 perlfaq4.html
字号:
<HTML> <HEAD> <TITLE>perlfaq4</TITLE> </HEAD> <BODY background="gback.jpg"><H1>perlfaq4 - perl常问问题集,第四篇</H1><p><p><H2><A NAME="INDEX">目录</A></H2><!-- INDEX BEGIN --><UL> <LI><A HREF="#NAME">NAME</A> <LI><A HREF="#DESCRIPTION">DESCRIPTION</A> <LI><A HREF="#Data_Numbers">Data: Numbers</A> <UL> <LI><A HREF="#Why_am_I_getting_long_decimals_">Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?</A> <LI><A HREF="#Why_isn_t_my_octal_data_interpre">Why isn't my octal data interpreted correctly?</A> <LI><A HREF="#Does_perl_have_a_round_function_">Does perl have a round function? What about ceil() and floor()?</A> <LI><A HREF="#How_do_I_convert_bits_into_ints_">How do I convert bits into ints?</A> <LI><A HREF="#How_do_I_multiply_matrices_">How do I multiply matrices?</A> <LI><A HREF="#How_do_I_perform_an_operation_on">How do I perform an operation on a series of integers?</A> <LI><A HREF="#How_can_I_output_Roman_numerals_">How can I output Roman numerals?</A> <LI><A HREF="#Why_aren_t_my_random_numbers_ran">Why aren't my random numbers random?</A> </UL> <LI><A HREF="#Data_Dates">Data: Dates</A> <UL> <LI><A HREF="#How_do_I_find_the_week_of_the_ye">How do I find the week-of-the-year/day-of-the-year?</A> <LI><A HREF="#How_can_I_compare_two_date_strin">How can I compare two date strings?</A> <LI><A HREF="#How_can_I_take_a_string_and_turn">How can I take a string and turn it into epoch seconds?</A> <LI><A HREF="#How_can_I_find_the_Julian_Day_">How can I find the Julian Day?</A> <LI><A HREF="#Does_Perl_have_a_year_2000_probl">Does Perl have a year 2000 problem?</A> </UL> <LI><A HREF="#Data_Strings">Data: Strings</A> <UL> <LI><A HREF="#How_do_I_validate_input_">How do I validate input?</A> <LI><A HREF="#How_do_I_unescape_a_string_">How do I unescape a string?</A> <LI><A HREF="#How_do_I_remove_consecutive_pair">How do I remove consecutive pairs of characters?</A> <LI><A HREF="#How_do_I_expand_function_calls_i">How do I expand function calls in a string?</A> <LI><A HREF="#How_do_I_find_matching_nesting_a">How do I find matching/nesting anything?</A> <LI><A HREF="#How_do_I_reverse_a_string_">How do I reverse a string?</A> <LI><A HREF="#How_do_I_expand_tabs_in_a_string">How do I expand tabs in a string?</A> <LI><A HREF="#How_do_I_reformat_a_paragraph_">How do I reformat a paragraph?</A> <LI><A HREF="#How_can_I_access_change_the_firs">How can I access/change the first N letters of a string?</A> <LI><A HREF="#How_do_I_change_the_Nth_occurren">How do I change the Nth occurrence of something?</A> <LI><A HREF="#How_can_I_count_the_number_of_oc">How can I count the number of occurrences of a substring within a string?</A> <LI><A HREF="#How_do_I_capitalize_all_the_word">How do I capitalize all the words on one line?</A> <LI><A HREF="#How_can_I_split_a_character_de">How can I split a [character] delimited string except when inside [character]? (Comma-separated files)</A> <LI><A HREF="#How_do_I_strip_blank_space_from_">How do I strip blank space from the beginning/end of a string?</A> <LI><A HREF="#How_do_I_extract_selected_column">How do I extract selected columns from a string?</A> <LI><A HREF="#How_do_I_find_the_soundex_value_">How do I find the soundex value of a string?</A> <LI><A HREF="#How_can_I_expand_variables_in_te">How can I expand variables in text strings?</A> <LI><A HREF="#What_s_wrong_with_always_quoting">What's wrong with always quoting "$vars"?</A> <LI><A HREF="#Why_don_t_my_HERE_documents_wo">Why don't my <<HERE documents work?</A> </UL> <LI><A HREF="#Data_Arrays">Data: Arrays</A> <UL> <LI><A HREF="#What_is_the_difference_between_">What is the difference between $array[1] and @array[1]?</A> <LI><A HREF="#How_can_I_extract_just_the_uniqu">How can I extract just the unique elements of an array?</A> <LI><A HREF="#How_can_I_tell_whether_an_array_">How can I tell whether an array contains a certain element?</A> <LI><A HREF="#How_do_I_compute_the_difference_">How do I compute the difference of two arrays? How do I compute the intersection of two arrays?</A> <LI><A HREF="#How_do_I_find_the_first_array_el">How do I find the first array element for which a condition is true?</A> <LI><A HREF="#How_do_I_handle_linked_lists_">How do I handle linked lists?</A> <LI><A HREF="#How_do_I_handle_circular_lists_">How do I handle circular lists?</A> <LI><A HREF="#How_do_I_shuffle_an_array_random">How do I shuffle an array randomly?</A> <LI><A HREF="#How_do_I_process_modify_each_ele">How do I process/modify each element of an array?</A> <LI><A HREF="#How_do_I_select_a_random_element">How do I select a random element from an array?</A> <LI><A HREF="#How_do_I_permute_N_elements_of_a">How do I permute N elements of a list?</A> <LI><A HREF="#How_do_I_sort_an_array_by_anyth">How do I sort an array by (anything)?</A> <LI><A HREF="#How_do_I_manipulate_arrays_of_bi">How do I manipulate arrays of bits?</A> <LI><A HREF="#Why_does_defined_return_true_o">Why does defined() return true on empty arrays and hashes?</A> </UL> <LI><A HREF="#Data_Hashes_Associative_Arrays">Data: Hashes (Associative Arrays)</A> <UL> <LI><A HREF="#How_do_I_process_an_entire_hash_">How do I process an entire hash?</A> <LI><A HREF="#What_happens_if_I_add_or_remove_">What happens if I add or remove keys from a hash while iterating over it?</A> <LI><A HREF="#How_do_I_look_up_a_hash_element_">How do I look up a hash element by value?</A> <LI><A HREF="#How_can_I_know_how_many_entries_">How can I know how many entries are in a hash?</A> <LI><A HREF="#How_do_I_sort_a_hash_optionally">How do I sort a hash (optionally by value instead of key)?</A> <LI><A HREF="#How_can_I_always_keep_my_hash_so">How can I always keep my hash sorted?</A> <LI><A HREF="#What_s_the_difference_between_d">What's the difference between "delete" and "undef" with hashes?</A> <LI><A HREF="#Why_don_t_my_tied_hashes_make_th">Why don't my tied hashes make the defined/exists distinction?</A> <LI><A HREF="#How_do_I_reset_an_each_operati">How do I reset an each() operation part-way through?</A> <LI><A HREF="#How_can_I_get_the_unique_keys_fr">How can I get the unique keys from two hashes?</A> <LI><A HREF="#How_can_I_store_a_multidimension">How can I store a multidimensional array in a DBM file?</A> <LI><A HREF="#How_can_I_make_my_hash_remember_">How can I make my hash remember the order I put elements into it?</A> <LI><A HREF="#Why_does_passing_a_subroutine_an">Why does passing a subroutine an undefined element in a hash create it?</A> <LI><A HREF="#How_can_I_make_the_Perl_equivale">How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?</A> <LI><A HREF="#How_can_I_use_a_reference_as_a_h">How can I use a reference as a hash key?</A> </UL> <LI><A HREF="#Data_Misc">Data: Misc</A> <UL> <LI><A HREF="#How_do_I_handle_binary_data_corr">How do I handle binary data correctly?</A> <LI><A HREF="#How_do_I_determine_whether_a_sca">How do I determine whether a scalar is a number/whole/integer/float?</A> <LI><A HREF="#How_do_I_keep_persistent_data_ac">How do I keep persistent data across program calls?</A> <LI><A HREF="#How_do_I_print_out_or_copy_a_rec">How do I print out or copy a recursive data structure?</A> <LI><A HREF="#How_do_I_define_methods_for_ever">How do I define methods for every class/object?</A> <LI><A HREF="#How_do_I_verify_a_credit_card_ch">How do I verify a credit card checksum?</A> </UL> <LI><A HREF="#AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</A></UL><!-- INDEX END --><HR><P><H2><A NAME="NAME">NAME</A></H2>perlfaq4 - Data Manipulation ($Revision: 1.19 $, $Date: 1997/04/24 22:43:57$)<P><P><HR><H2><A NAME="DESCRIPTION">DESCRIPTION</A></H2>The section of the <FONT SIZE=-1>FAQ</FONT> answers question related to the manipulation ofdata as numbers, dates, strings, arrays, hashes, and miscellaneous dataissues.<P><P><HR><H2><A NAME="Data_Numbers">Data: Numbers</A></H2><P><HR><H3><A NAME="Why_am_I_getting_long_decimals_">Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?</A></H3>Internally, your computer represents floating-point numbers in binary.Floating-point numbers read in from a file, or appearing as literals inyour program, are converted from their decimal floating-pointrepresentation (eg, 19.95) to the internal binary representation.<P>However, 19.95 can't be precisely represented as a binary floating-pointnumber, just like 1/3 can't be exactly represented as a decimalfloating-point number. The computer's binary representation of 19.95,therefore, isn't exactly 19.95.<P>When a floating-point number gets printed, the binary floating-pointrepresentation is converted back to decimal. These decimal numbers aredisplayed in either the format you specify with <CODE>printf(),</CODE> orthe current output format for numbers (see <A HREF="../../tppmsgs/msgs1.htm#100" tppabs="http://www.perl.org/CPAN/doc/manual/html/pod/perlvar.html#_">$#</A> if you use print. <CODE>$#</CODE> has a different default value in Perl5 than it did in Perl4. Changing <CODE>$#</CODE> yourself is deprecated.<P>This affects <STRONG>all</STRONG> computer languages that represent decimal floating-point numbers in binary,not just Perl. Perl provides arbitrary-precision decimal numbers with theMath::BigFloat module (part of the standard Perl distribution), butmathematical operations are consequently slower.<P>To get rid of the superfluous digits, just use a format (eg,<CODE>printf("%.2f", 19.95)</CODE>) to get the required precision.<P><P><HR><H3><A NAME="Why_isn_t_my_octal_data_interpre">Why isn't my octal data interpreted correctly?</A></H3>Perl only understands octal and hex numbers as such when they occur as literals in your program. If they are read in from somewhere and assigned, no automatic conversion takes place. You must explicitly use <CODE>oct()</CODE> or <CODE>hex()</CODE> if you want the values converted. <CODE>oct()</CODE> interprets both hex (``0x350'') numbers and octal ones (``0350'' or even without the leading ``0'', like ``377''), while <CODE>hex()</CODE> only converts hexadecimal ones, with or without a leading ``0x'', like ``0x255'', <FONT SIZE=-1>``3A'',</FONT> ``ff'', or ``deadbeef''.<P>This problem shows up most often when people try using<CODE>chmod(),</CODE> <CODE>mkdir(),</CODE> <CODE>umask(),</CODE> or<CODE>sysopen(),</CODE> which all want permissions in octal.<P><PRE> chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right</PRE><P><P><HR><H3><A NAME="Does_perl_have_a_round_function_">Does perl have a round function? What about ceil() and floor()?Trig functions?</A></H3>For rounding to a certain number of digits, <CODE>sprintf()</CODE> or<CODE>printf()</CODE> is usually the easiest route.<P>The <FONT SIZE=-1>POSIX</FONT> module (part of the standard perl distribution)implements <CODE>ceil(),</CODE> <CODE>floor(),</CODE> and a number of othermathematical and trigonometric functions.<P>In 5.000 to 5.003 Perls, trigonometry was done in the Math::Complex module.With 5.004, the Math::Trig module (part of the standard perl distribution)implements the trigonometric functions. Internally it uses theMath::Complex module and some functions can break out from the real axisinto the complex plane, for example the inverse sine of 2.<P>Rounding in financial applications can have serious implications, and therounding method used should be specified precisely. In these cases, itprobably pays not to trust whichever system rounding is being used by Perl,but to instead implement the rounding function you need yourself.<P><P><HR><H3><A NAME="How_do_I_convert_bits_into_ints_">How do I convert bits into ints?</A></H3>To turn a string of 1s and 0s like '10110110' into a scalar containing itsbinary value, use the <CODE>pack()</CODE> function (documented in<A HREF="../../tppmsgs/msgs0.htm#68" tppabs="http://www.perl.org/CPAN/doc/manual/html/pod/perlfunc.html#pack">pack</A>):<P><PRE> $decimal = pack('B8', '10110110');</PRE><P>Here's an example of going the other way:<P><PRE> $binary_string = join('', unpack('B*', "\x29"));</PRE><P><P><HR><H3><A NAME="How_do_I_multiply_matrices_">How do I multiply matrices?</A></H3>Use the Math::Matrix or Math::MatrixReal modules (available from <FONT SIZE=-1>CPAN)</FONT> or the <FONT SIZE=-1>PDL</FONT> extension (also available from <FONT SIZE=-1>CPAN).</FONT><P><P><HR><H3><A NAME="How_do_I_perform_an_operation_on">How do I perform an operation on a series of integers?</A></H3>To call a function on each element in an array, and collect the results,use:<P><PRE> @results = map { my_func($_) } @array;</PRE><P>For example:<P><PRE> @triple = map { 3 * $_ } @single;</PRE><P>To call a function on each element of an array, but ignore the results:<P><PRE> foreach $iterator (@array) { &my_func($iterator); }</PRE><P>To call a function on each integer in a (small) range, you <STRONG>can</STRONG> use:<P><PRE> @results = map { &my_func($_) } (5 .. 25);</PRE><P>but you should be aware that the <CODE>..</CODE> operator creates an array of all integers in the range. This can take a lotof memory for large ranges. Instead use:<P><PRE> @results = (); for ($i=5; $i < 500_005; $i++) { push(@results, &my_func($i)); }</PRE><P><P><HR><H3><A NAME="How_can_I_output_Roman_numerals_">How can I output Roman numerals?</A></H3>Get the <AHREF="../../tppmsgs/msgs1.htm#133" tppabs="http://www.perl.com/CPAN/modules/by-module/Roman">http://www.perl.com/CPAN/modules/by-module/Roman</A>module.<P><P><HR><H3><A NAME="Why_aren_t_my_random_numbers_ran">Why aren't my random numbers random?</A></H3>The short explanation is that you're getting pseudorandom numbers, not random ones, because that's how these things work. <FONT SIZE=-1>A</FONT> longer explanation is available on <A HREF="../../tppmsgs/msgs1.htm#134" tppabs="http://www.perl.com/CPAN/doc/FMTEYEWTK/random">http://www.perl.com/CPAN/doc/FMTEYEWTK/random</A>, courtesy of Tom Phoenix.<P>You should also check out the Math::TrulyRandom module from <FONT SIZE=-1>CPAN.</FONT><P><P><HR><H2><A NAME="Data_Dates">Data: Dates</A></H2><P><HR><H3><A NAME="How_do_I_find_the_week_of_the_ye">How do I find the week-of-the-year/day-of-the-year?</A></H3>The day of the year is in the array returned by <CODE>localtime()</CODE>(see<A HREF="../../tppmsgs/msgs0.htm#68" tppabs="http://www.perl.org/CPAN/doc/manual/html/pod/perlfunc.html#localtime">localtime</A>):<P><PRE> $day_of_year = (localtime(time()))[7];</PRE><P>or more legibly (in 5.004 or higher):<P><PRE> use Time::localtime; $day_of_year = localtime(time())->yday;</PRE><P>You can find the week of the year by dividing this by 7:<P><PRE> $week_of_year = int($day_of_year / 7);</PRE><P>Of course, this believes that weeks start at zero.<P><P><HR><H3><A NAME="How_can_I_compare_two_date_strin">How can I compare two date strings?</A></H3>Use the Date::Manip or Date::DateCalc modules from <FONT SIZE=-1>CPAN.</FONT><P><P><HR><H3><A NAME="How_can_I_take_a_string_and_turn">How can I take a string and turn it into epoch seconds?</A></H3>If it's a regular enough string that it always has the same format, you can split it up and pass the parts to timelocal in the standard Time::Local module. Otherwise, you should look into one of the Date modules from <FONT SIZE=-1>CPAN.</FONT><P><P><HR><H3><A NAME="How_can_I_find_the_Julian_Day_">How can I find the Julian Day?</A></H3>Neither Date::Manip nor Date::DateCalc deal with Julian days. Instead,there is an example of Julian date calculation in <AHREF="../../tppmsgs/msgs1.htm#135" tppabs="http://www.perl.com/CPAN/authors/David_Muir_Sharnoff/modules/Time/JulianDay.pm.gz">http://www.perl.com/CPAN/authors/David_Muir_Sharnoff/modules/Time/JulianDay.pm.gz</A>,which should help.<P><P><HR><H3><A NAME="Does_Perl_have_a_year_2000_probl">Does Perl have a year 2000 problem?</A></H3>Not unless you use Perl to create one. The date and time functions suppliedwith perl (gmtime and localtime) supply adequate information to determinethe year well beyond 2000 (2038 is when trouble strikes). The year returnedby these functions when used in an array context is the year minus 1900.For years between 1910 and 1999 this <EM>happens</EM>to be a 2-digit decimal number. To avoid the year 2000 problem simply donot treat the year as a 2-digit number. It isn't.<P>When <CODE>gmtime()</CODE> and <CODE>localtime()</CODE> are used in ascalar context they return a timestamp string that contains afully-expanded year. For example,<CODE>$timestamp = gmtime(1005613200)</CODE> sets <CODE>$timestamp</CODE> to ``Tue Nov 13 01:00:00 2001''. There's noyear 2000 problem here.<P><P><HR><H2><A NAME="Data_Strings">Data: Strings</A></H2><P><HR><H3><A NAME="How_do_I_validate_input_">How do I validate input?</A></H3>The answer to this question is usually a regular expression, perhaps withauxiliary logic. See the more specific questions (numbers, email addresses,etc.) for details.<P><P><HR><H3><A NAME="How_do_I_unescape_a_string_">How do I unescape a string?</A></H3>It depends just what you mean by ``escape''. <FONT SIZE=-1>URL</FONT> escapes are dealt with in <A HREF="perlfaq9.html" tppabs="http://202.96.217.5/~xiaoyi/perlfaq9.html">the perlfaq9 manpage</A>. Shell escapes with the backslash (\) character are removed with:<P><PRE> s/\\(.)/$1/g;</PRE><P>Note that this won't expand \n or \t or any other special escapes.<P><P><HR><H3><A NAME="How_do_I_remove_consecutive_pair">How do I remove consecutive pairs of characters?</A></H3>To turn ``abbcccd'' into ``abccd'':<P><PRE> s/(.)\1/$1/g;</PRE><P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -