Hashes are unordered collections of scalar values indexed by their associated string key.I spend more time working with text than anything else. Normal arrays are ordered lists of scalars indexed by number, starting with 0. A scalar is a single string (of any size, limited only by the available memory), number, or a reference to something. Perl has three built-in data types: scalars, arrays of scalars, and associative arrays of scalars, known as "hashes". The Perl documentation ( perldata) states: Now let's get back to the name of that hash. So the hash and all its elements are created as needed without any explicit code to create them. And performing arithmetic on a value that is undefined is the same as using zero. "BLOCK" is false, path already added to outputĪs in AWK, referencing a hash (aka an associative array) in Perl that does not exist, automatically creates it. To recap, in pseudo-code, the block looks like this: After the hash element is tested, it is incremented via the ++ operator, so that the next time the path element is seen, the expression will be false, and the duplicate path element will be skipped. If the hash element is non-zero, the path has already been seen, the expression is false, and the path element is not added to the list. Therefore, the current path element has not been seen before, and it is added to the list that grep is creating. If the hash element is zero, the expression is true (due to the negation). In this case, the EXPR is actually a BLOCK (code in curly braces. Perl's grep function evaluates its EXPR for each item in the LIST, and when the expression is true, the current item from the list is added to a new list that will become the result of the grep function. Note that this is not the command-line grep this is the Perl grep function. Without the parentheses, the call would be seen as chomp ( $_ ) =, which is invalid syntax. The parentheses in the call to chomp is to change the precedence of the terms in the expression. So how come the chomp function used parentheses around its arguments and the split function doesn't? Actually, the chomp function didn't use parentheses around its arguments the syntax of Perl function calls is simply func args, no parentheses. If you remember from the first post, the AWK version changed the record separator to a colon to split the path into individual elements in this version, the split function is used to accomplish that. The split function produces a list for grep to work on. It operates on the pattern space since no other specific data is provided. In other words, it separates the path into individual path elements. This should be reasonably obvious: this splits the pattern space on colons. If you remember from the first post, the newline was eliminated by adding the -n option to echo. All that was a long-winded way of saying that the first line of code removes that pesky newline from the input. The null file handle, in this case the standard input via bash's assigns the path to the pattern space, then the chomp function removes a record separator from the end of the pattern space. The argument to chomp is an assignment expression that assigns the null file handle to Perl's "default input and pattern-searching space", which is the variable named $_. The default record separator is a newline, as it is in AWK. Chomp is pretty straightforward it removes a single "record separator" from each of its arguments.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |