perlfaq4.html

来自「perl教程」· HTML 代码 · 共 867 行 · 第 1/5 页

HTML
867
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlfaq4 - Data Manipulation</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlfaq4 - Data Manipulation</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#data__numbers">Data: Numbers</a></li>
	<ul>

		<li><a href="#why_am_i_getting_long_decimals__eg__19_9499999999999__instead_of_the_numbers_i_should_be_getting__eg__19_95_">Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?</a></li>
		<li><a href="#why_is_int___broken">Why is <a href="../../lib/Pod/perlfunc.html#item_int"><code>int()</code></a> broken?</a></li>
		<li><a href="#why_isn_t_my_octal_data_interpreted_correctly">Why isn't my octal data interpreted correctly?</a></li>
		<li><a href="#does_perl_have_a_round___function_what_about_ceil___and_floor___trig_functions">Does Perl have a <code>round()</code> function?  What about <code>ceil()</code> and floor()?  Trig functions?</a></li>
		<li><a href="#how_do_i_convert_between_numeric_representations_bases_radixes">How do I convert between numeric representations/bases/radixes?</a></li>
		<li><a href="#why_doesn_t___work_the_way_i_want_it_to">Why doesn't &amp; work the way I want it to?</a></li>
		<li><a href="#how_do_i_multiply_matrices">How do I multiply matrices?</a></li>
		<li><a href="#how_do_i_perform_an_operation_on_a_series_of_integers">How do I perform an operation on a series of integers?</a></li>
		<li><a href="#how_can_i_output_roman_numerals">How can I output Roman numerals?</a></li>
		<li><a href="#why_aren_t_my_random_numbers_random">Why aren't my random numbers random?</a></li>
		<li><a href="#how_do_i_get_a_random_number_between_x_and_y">How do I get a random number between X and Y?</a></li>
	</ul>

	<li><a href="#data__dates">Data: Dates</a></li>
	<ul>

		<li><a href="#how_do_i_find_the_day_or_week_of_the_year">How do I find the day or week of the year?</a></li>
		<li><a href="#how_do_i_find_the_current_century_or_millennium">How do I find the current century or millennium?</a></li>
		<li><a href="#how_can_i_compare_two_dates_and_find_the_difference">How can I compare two dates and find the difference?</a></li>
		<li><a href="#how_can_i_take_a_string_and_turn_it_into_epoch_seconds">How can I take a string and turn it into epoch seconds?</a></li>
		<li><a href="#how_can_i_find_the_julian_day">How can I find the Julian Day?</a></li>
		<li><a href="#how_do_i_find_yesterday_s_date">How do I find yesterday's date?</a></li>
		<li><a href="#does_perl_have_a_year_2000_problem_is_perl_y2k_compliant">Does Perl have a Year 2000 problem?  Is Perl Y2K compliant?</a></li>
	</ul>

	<li><a href="#data__strings">Data: Strings</a></li>
	<ul>

		<li><a href="#how_do_i_validate_input">How do I validate input?</a></li>
		<li><a href="#how_do_i_unescape_a_string">How do I unescape a string?</a></li>
		<li><a href="#how_do_i_remove_consecutive_pairs_of_characters">How do I remove consecutive pairs of characters?</a></li>
		<li><a href="#how_do_i_expand_function_calls_in_a_string">How do I expand function calls in a string?</a></li>
		<li><a href="#how_do_i_find_matching_nesting_anything">How do I find matching/nesting anything?</a></li>
		<li><a href="#how_do_i_reverse_a_string">How do I reverse a string?</a></li>
		<li><a href="#how_do_i_expand_tabs_in_a_string">How do I expand tabs in a string?</a></li>
		<li><a href="#how_do_i_reformat_a_paragraph">How do I reformat a paragraph?</a></li>
		<li><a href="#how_can_i_access_or_change_n_characters_of_a_string">How can I access or change N characters of a string?</a></li>
		<li><a href="#how_do_i_change_the_nth_occurrence_of_something">How do I change the Nth occurrence of something?</a></li>
		<li><a href="#how_can_i_count_the_number_of_occurrences_of_a_substring_within_a_string">How can I count the number of occurrences of a substring within a string?</a></li>
		<li><a href="#how_do_i_capitalize_all_the_words_on_one_line">How do I capitalize all the words on one line?</a></li>
		<li><a href="#how_can_i_split_a__character__delimited_string_except_when_inside__character_">How can I split a [character] delimited string except when inside [character]?</a></li>
		<li><a href="#how_do_i_strip_blank_space_from_the_beginning_end_of_a_string">How do I strip blank space from the beginning/end of a string?</a></li>
		<li><a href="#how_do_i_pad_a_string_with_blanks_or_pad_a_number_with_zeroes">How do I pad a string with blanks or pad a number with zeroes?</a></li>
		<li><a href="#how_do_i_extract_selected_columns_from_a_string">How do I extract selected columns from a string?</a></li>
		<li><a href="#how_do_i_find_the_soundex_value_of_a_string">How do I find the soundex value of a string?</a></li>
		<li><a href="#how_can_i_expand_variables_in_text_strings">How can I expand variables in text strings?</a></li>
		<li><a href="#what_s_wrong_with_always_quoting__vars">What's wrong with always quoting &quot;$vars&quot;?</a></li>
		<li><a href="#why_don_t_my___here_documents_work">Why don't my &lt;&lt;HERE documents work?</a></li>
	</ul>

	<li><a href="#data__arrays">Data: Arrays</a></li>
	<ul>

		<li><a href="#what_is_the_difference_between_a_list_and_an_array">What is the difference between a list and an array?</a></li>
		<li><a href="#what_is_the_difference_between__array_1__and__array_1_">What is the difference between $array[1] and @array[1]?</a></li>
		<li><a href="#how_can_i_remove_duplicate_elements_from_a_list_or_array">How can I remove duplicate elements from a list or array?</a></li>
		<li><a href="#how_can_i_tell_whether_a_certain_element_is_contained_in_a_list_or_array">How can I tell whether a certain element is contained in a list or array?</a></li>
		<li><a href="#how_do_i_compute_the_difference_of_two_arrays_how_do_i_compute_the_intersection_of_two_arrays">How do I compute the difference of two arrays?  How do I compute the intersection of two arrays?</a></li>
		<li><a href="#how_do_i_test_whether_two_arrays_or_hashes_are_equal">How do I test whether two arrays or hashes are equal?</a></li>
		<li><a href="#how_do_i_find_the_first_array_element_for_which_a_condition_is_true">How do I find the first array element for which a condition is true?</a></li>
		<li><a href="#how_do_i_handle_linked_lists">How do I handle linked lists?</a></li>
		<li><a href="#how_do_i_handle_circular_lists">How do I handle circular lists?</a></li>
		<li><a href="#how_do_i_shuffle_an_array_randomly">How do I shuffle an array randomly?</a></li>
		<li><a href="#how_do_i_process_modify_each_element_of_an_array">How do I process/modify each element of an array?</a></li>
		<li><a href="#how_do_i_select_a_random_element_from_an_array">How do I select a random element from an array?</a></li>
		<li><a href="#how_do_i_permute_n_elements_of_a_list">How do I permute N elements of a list?</a></li>
		<li><a href="#how_do_i_sort_an_array_by__anything_">How do I sort an array by (anything)?</a></li>
		<li><a href="#how_do_i_manipulate_arrays_of_bits">How do I manipulate arrays of bits?</a></li>
		<li><a href="#why_does_defined___return_true_on_empty_arrays_and_hashes">Why does <a href="../../lib/Pod/perlfunc.html#item_defined"><code>defined()</code></a> return true on empty arrays and hashes?</a></li>
	</ul>

	<li><a href="#data__hashes__associative_arrays_">Data: Hashes (Associative Arrays)</a></li>
	<ul>

		<li><a href="#how_do_i_process_an_entire_hash">How do I process an entire hash?</a></li>
		<li><a href="#what_happens_if_i_add_or_remove_keys_from_a_hash_while_iterating_over_it">What happens if I add or remove keys from a hash while iterating over it?</a></li>
		<li><a href="#how_do_i_look_up_a_hash_element_by_value">How do I look up a hash element by value?</a></li>
		<li><a href="#how_can_i_know_how_many_entries_are_in_a_hash">How can I know how many entries are in a hash?</a></li>
		<li><a href="#how_do_i_sort_a_hash__optionally_by_value_instead_of_key_">How do I sort a hash (optionally by value instead of key)?</a></li>
		<li><a href="#how_can_i_always_keep_my_hash_sorted">How can I always keep my hash sorted?</a></li>
		<li><a href="#what_s_the_difference_between_delete_and_undef_with_hashes">What's the difference between &quot;delete&quot; and &quot;undef&quot; with hashes?</a></li>
		<li><a href="#why_don_t_my_tied_hashes_make_the_defined_exists_distinction">Why don't my tied hashes make the defined/exists distinction?</a></li>
		<li><a href="#how_do_i_reset_an_each___operation_partway_through">How do I reset an <a href="../../lib/Pod/perlfunc.html#item_each"><code>each()</code></a> operation part-way through?</a></li>
		<li><a href="#how_can_i_get_the_unique_keys_from_two_hashes">How can I get the unique keys from two hashes?</a></li>
		<li><a href="#how_can_i_store_a_multidimensional_array_in_a_dbm_file">How can I store a multidimensional array in a DBM file?</a></li>
		<li><a href="#how_can_i_make_my_hash_remember_the_order_i_put_elements_into_it">How can I make my hash remember the order I put elements into it?</a></li>
		<li><a href="#why_does_passing_a_subroutine_an_undefined_element_in_a_hash_create_it">Why does passing a subroutine an undefined element in a hash create it?</a></li>
		<li><a href="#how_can_i_make_the_perl_equivalent_of_a_c_structure_c___class_hash_or_array_of_hashes_or_arrays">How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?</a></li>
		<li><a href="#how_can_i_use_a_reference_as_a_hash_key">How can I use a reference as a hash key?</a></li>
	</ul>

	<li><a href="#data__misc">Data: Misc</a></li>
	<ul>

		<li><a href="#how_do_i_handle_binary_data_correctly">How do I handle binary data correctly?</a></li>
		<li><a href="#how_do_i_determine_whether_a_scalar_is_a_number_whole_integer_float">How do I determine whether a scalar is a number/whole/integer/float?</a></li>
		<li><a href="#how_do_i_keep_persistent_data_across_program_calls">How do I keep persistent data across program calls?</a></li>
		<li><a href="#how_do_i_print_out_or_copy_a_recursive_data_structure">How do I print out or copy a recursive data structure?</a></li>
		<li><a href="#how_do_i_define_methods_for_every_class_object">How do I define methods for every class/object?</a></li>
		<li><a href="#how_do_i_verify_a_credit_card_checksum">How do I verify a credit card checksum?</a></li>
		<li><a href="#how_do_i_pack_arrays_of_doubles_or_floats_for_xs_code">How do I pack arrays of doubles or floats for XS code?</a></li>
	</ul>

	<li><a href="#author_and_copyright">AUTHOR AND COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlfaq4 - Data Manipulation ($Revision: 1.73 $, $Date: 2005/12/31 00:54:37 $)</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This section of the FAQ answers questions related to manipulating
numbers, dates, strings, arrays, hashes, and miscellaneous data issues.</p>
<p>
</p>
<hr />
<h1><a name="data__numbers">Data: Numbers</a></h1>
<p>
</p>
<h2><a name="why_am_i_getting_long_decimals__eg__19_9499999999999__instead_of_the_numbers_i_should_be_getting__eg__19_95_">Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?</a></h2>
<p>Internally, your computer represents floating-point numbers
in binary. Digital (as in powers of two) computers cannot
store all numbers exactly.  Some real numbers lose precision
in the process.  This is a problem with how computers store
numbers and affects all computer languages, not just Perl.</p>
<p><a href="../../lib/Pod/perlnumber.html">the perlnumber manpage</a> show the gory details of number
representations and conversions.</p>
<p>To limit the number of decimal places in your numbers, you
can use the printf or sprintf function.  See the
<a href="../../lib/Pod/perlop.html">&quot;Floating Point Arithmetic&quot;</a> for more details.</p>
<pre>
        <span class="keyword">printf</span> <span class="string">"%.2f"</span><span class="operator">,</span> <span class="number">10</span><span class="operator">/</span><span class="number">3</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$number</span> <span class="operator">=</span> <span class="keyword">sprintf</span> <span class="string">"%.2f"</span><span class="operator">,</span> <span class="number">10</span><span class="operator">/</span><span class="number">3</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="why_is_int___broken">Why is <a href="../../lib/Pod/perlfunc.html#item_int"><code>int()</code></a> broken?</a></h2>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?