Perl chomp Function
NEW!!!
Check my new resource: Perl How To Tutorial eBooks
Perl chomp Menu:
1. The syntax forms 1.1. chomp VARIABLE 1.2. chomp (LIST) 1.3. chomp
We’ll examine below Perl chomp function and how to use it in different contexts. The snippet code examples will point out various ways of using it and will clarify and highlight some interesting particular aspects. Please take your time and have a look at the following considerations, which will help you understand better its features.There are three syntax forms for this function: chomp VARIABLE
chomp (LIST)
chomp
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
If we use the first syntax form, we call this function having as argument the name of a variable. In this case the function will remove any trailing string at the end of the variable. Perl chomp function will not remove all whitespace characters by default, but only the ones that match the trailing string currently stored in the special variable $/ - also known as the input record separator. If there is not any trailing character at the end of the variable, the variable will remain unchanged. Please note that there are significant differences between Perl chomp and Perl chop functions. While a chop function deletes the last ending variable character regardless of whatever it is, a chomp function checks whether the last character matches the input line separator and only then it deletes it. Perl chomp function returns the number of characters removed. This function is far more than mere a common function and you’ll use it especially when you are reading some text from the keyboard through the special file handle STDIN. The standard input stream will add a newline character (usually\n) at the end of each line, and you can get rid of it very simply by calling the chomp function. No need to use any sophisticated regular expression for this.
For instance, look at the next code: print "User name: ";
$name = <STDIN>;
chomp $name;
print "Your user name is : $name\n";
This code will read from keyboard the user name and it will store it in the variable $name. This variable will have at the end a newline character, inserted after the user was hitting the return key. The chomp function will take it off.You can actually use chomp function with an lvalue, including an assignment. For instance, in the example above, we could replace the line two and three with the next line: Generally, Perl chomp() function removes only the last newline character but what if there are more? In this case you can play with the special variable $/, using the paragraph mode by setting $/ to "", as in the next example:$/ = "";
$v = "\n\nsome text here\n\n\n\n";
$nr = chomp $v;
After executing this code, the first two newlines will remain unchanged, but the last fourth will be removed. The variable $nr will be set to 4 – the number of newline characters removed. Be careful, however, when you alter the content of the special variable $/, and restore it to its default value (\n), when you consider necessary.
The second syntax form is for lists. You can chomp any array or hash (associative array), but we will be examining only one-dimensional ones here, without any reference. It doesn’t matter if you refer to arrays or hashes but don’t forget to put the array or hash inside the parenthesis of the Perl chomp function, otherwise the result could be one that you wouldn’t expect. For beginning with, we will consider the case of arrays. To illustrate how to use Perl chomp function with an array, take a moment to examine the next snippet code: $nr = chomp(@colors = <STDIN>);
print "\@colors = @colors\n";
print "Newlines removed: $nr";
This code will read all lines up to EOF and will chomp them off; the number of newlines removed is equal with the number of lines read. Here is an example of the output: C:\Perl\bin>perl chomptst.pl
red
yellow
white
blue
^Z
@colors = red yellow white blue
Newlines removed: 4
Another way to do this is by using a while loop, if you want:@colors = ();
while($line = <STDIN>)
{
$nr+=chomp $line;
push(@colors, $line);
}
print "\@colors = @colors\n";
print "Newlines removed: $nr";
The output will be identical as in the previous example shown above.Next, let’s see an example when the Perl chomp function is called with a hash as argument. This will be illustrated in the following code segment. But before seeing the example, I want to specify that, like as in the case of the chop() function, only the values of a hash are chomped, the keys being left unchanged. After processing all the elements of a hash, the Perl chomp function will return the number of chomped characters. # read the pair elements of the hash from STDIN
chomp(%fruitsColors = <STDIN>);
# the next loop prints the keys/values of the hash
foreach $key (keys %fruitsColors)
{
print "Key: $key, Value: $fruitsColors{$key}\n";
}
For this code, here is an output example:C:\Perl\bin>perl chomptst.pl
apricot
yellow
cherry
red
plum
dark blue
^Z in Windows (^d in Linux)
Key: plum
, Value: dark blue
Key: apricot
, Value: yellow
Key: cherry
, Value: red
As you see in the example above, the keys haven’t been chomped (by instance, the key "plum" ends with a newline character and because of this its corresponding value was printed on a new line in the cmd window). Let’s look at the same example, but in this case we will initialize the hash by coding: # create a new hash – fruitsColors
# and assigning some elements to it
$fruitsColors{"plum\n"}="dark blue\n";
$fruitsColors{"apricot\n"}="yellow\n";
$fruitsColors{"cherry\n"}="red\n";
# chomp the hash
$nr = chomp(%fruitsColors);
# the next loop prints the keys/values of the hash
foreach $key (keys %fruitsColors)
{
print "Key: $key, Value: $fruitsColors{$key}\n";
}
print "newlines removed: $nr\n";
The output for this short script:C:\Perl\bin>chomptst.pl
Key: apricot
, Value: yellow
Key: plum
, Value: dark blue
Key: cherry
, Value: red
newlines removed: 3
As you see in this example, the number of newlines removed was 3 meanwhile the Perl hash keys remained "unchomped".
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
The last syntax form of Perl chomp function is that when the argument variable is omitted. Let’s work around through one of the examples shown below. Quick example … # define an array
@numbers = ("one\n", "two\n", "three\n", "four\n");
# let’s loop through its elements using foreach
foreach (@numbers) {
chomp;
}
print "@numbers\n";
# displays one two three four
The foreach statement loop variable is missing and that means that foreach will use for iteration the special variable $_. At each step of iteration:- the current element of the array will be stored in $_
- the value stored in $_ will be chomped by its ending symbol
As a rule of thumb, we conclude that Perl chomp() function avoids uncertainty about whether a line of input has a newline character or not. If you use <STDIN> handle to input some information, you should always "chomp" the line of input immediately after reading it.
***Well, you can use the Perl chomp function in conjunction with the $/ (the input record separator) in many ways and in different contexts. The most frequently used are described in my Perl "Perl Scalar and String Functions - How To Tutorial" where I show you how to play with this important function in detail. Are you still with me? I’ll point out below a few ways to implement it. I haven’t got enough space to describe all this here (my eBook counts more than 160 pages), but I’m sure you’ll get some useful ideas if you follow me. In this eBook I illustrate each of the following topics with a few snippet codes. If some of the examples are simple enough to help you understand the topic better, others are more complex and use different topics to achieve the specific tasks. From string and array functions to regular expressions, you’ll find a lot of topics throughout this eBook. All the topics mentioned below are covered through complete examples. Read a file as a chomped array You can read a file in an array in a single line of code and chomp the array globally. This is not very suitable for a large file because the whole file is read into the array and the array is chomped only afterwards. chomp the array elements one by one There are more approaches to chomp the elements of an array one by one. The three examples provided in this eBook show you how to chomp the array elements one by one using foreach, while and map. chomp a hash If you remember, the chomp function doesn’t act on the hash keys, only the values are chomped. You will find a few examples that help you chomp the keys of a hash. chomp and $/ By default, the $/ is set to the newline character, i.e. "\n". You can play with the input record separator to perform different tasks, as follows: - slurp a file into one scalar variable – by setting $/ to undef
- read a text file in paragraph mode – by setting $/ to ""
- read a binary file – by setting $/ to "\0" character; I give you an example of pseudocode for creating and reading a binary file if you just want to code it by yourself (or you can check the script example from my eBook):
- initialize an array with a few string elements
- create a binary file from the array with the records delimitated by "\0" separator (each string element of the array is converted into its corresponding hexadecimal value that is appended onto the binary file)
- empty the initial array
- set the $/ to "\0"
- read the binary file line by line, chomp the "\0" rightmost character from the current line, convert the hex string read from the file record back into its character string and append this line to our array
- print the content of the array (Notice that we’ll get back the initial content of our array)
chomp a reference variable You can use chomp with scalar, array or hash references. For example, to chomp an array reference you must first dereference the reference by using the notation @{$arrayRef}. You will get a complete example for each case as well as a few other ways to chomp the keys of a hash when a hash is defined by a reference.
Using chomp when reading from STDIN Find out how to use chomp in connection with the diamond operator <>. You’ll see how to chomp text files either by supplying their names in the command line or by setting the elements of the @ARGV special variable inside a Perl script. chomp in connection with the while and foreach statements There are many approaches to reading some lines from STDIN and then chomping their ending newlines. I exemplify by using the foreach and while loop statements. chomp and the loop controls In order to change the loop behavior, the Perl language provides three loop control operators: last, next and redo. I provide an example about how you can use them in conjunction with the Perl chomp function. chomp large data If you have a file with large amount of data you can’t simply slurp the file in an array and globally chomp it because this can eat a big chunk of memory and you could run into some memory issues. A way to consider is using the while loop statement and I give you a complete example about how to achieve this. chomp and push You can use the push function if you need to read some lines one by one from STDIN, chomp and store them in an array. You can see how to implement it in a snippet script. chomp and split In the eBook tutorial I show you an example about how to use the chomp and split functions to read a csv file line by line, chomp each line and finally split the content of the line into different variables. Please remember that a csv file is a comma delimited text data file that has the extension 'csv' and there are a lot of applications such as Excel and MS Access that allow you to save the data in a csv format. chomp multidimensional arrays or hashes Please note that in Perl there are no such things as multidimensional arrays or hashes. All Perl arrays and hashes are internally one-dimensional. They can only store scalar values, meaning strings, numbers or references. They cannot contain other arrays or hashes, but can contain references to other arrays or hashes. Using the mechanism of references, you can easily emulate multidimensional structures in Perl. The particular cases are array of arrays, array of hashes, hash of hashes and hash of arrays. Using the references model, you can build an array of arrays of hashes and so on; it depends on your needs. In this part of the eBook I give you an example where I use a recursive subroutine to go through complex data structures and individually chomp those elements that are not references. The subroutine has a scalar value as its single argument. If an array/hash reference is found, the subroutine is invoked again for all the elements of the array/hash, using the foreach loop statement. If the argument is not a reference, the corresponding value will be chomped. To avoid an infinite loop, the subroutine checks if the references are unique – if a reference appears in the complex data structure more than once, the script will print the respective reference and it will stop. To check if a reference is unique I use a hash where I store the references as hash keys. In my eBook I exemplify how to call this subroutine using as argument either a reference to an array of arrays or a reference to a hash of hashes. Obviously, you can use this subroutine to perform whatever you want instead of chomp by modifying the code appropriately. Well, there are much more ... Have a look at the Table of Contents of the "Perl Scalar and String Functions - How To Tutorial" eBook.
Subscribe now to my FREE newsletter (regarding tips and tricks to manipulate multidimensional arrays and hashes in Perl) and you'll receive a link to download for FREE my Perl chomp Function Tutorial eBook (a $7.50 value), it's a bonus for you and I hope you'll enjoy it. You can see its Table of Contents here (if you subscribe to my free newsletter, you don't have to pay for this eBook or use your paypal account).
Don't forget to check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :
Perl How To Tutorial eBooks
Table of Contents:
A Perl Script Install Perl Running Perl Perl Data Types Perl Variables Perl Operators Perl Lists Perl Arrays Array Size Array Length Perl Hashes Perl Statements Perl if Perl unless Perl switch Perl while Perl do-while Perl until Perl do-until Perl for Perl foreach Built-in Perl Functions Functions by Category String Functions Array Functions Functions in alphabetical order chomp chop chr crypt defined grep hex index join lc lcfirst length map oct ord pack pop push qw reverse rindex scalar shift sort splice split substr uc undef unshift
return from Perl chomp function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!


|