In general
- any Perl function or subroutine that returns an array can return a count instead by prefixing it with scalar. Thus:
- scalar @aFiles returns the number of items in @aFiles
- scalar grep returns the number of matches instead of the actual matches.
- scalar keys returns the number of keys in a hash rather than the actual keys.
- if you need to match the entire value, start your regex with ^ and end it with a $. /a/ matches any string with the letter a. /^a$/ matches only the string "a".
- next if ... lets you skip a loop iteration if whatever comes after "if" is true
So applying this to you little code snippet:
for my $k (sort keys %$hFiles) {
# ^$k$ forces $k to match the entire value of each entry in @aFiles
# - ^ forces match at start of entry
# - $ forces match at end of entry
# - ^$k$ expands to whatever is in $k,
# i.e. $k="x", then expands to ^x$
#
# next if scalar grep... will go back to start of loop whenever one
# or more values match
# - scalar grep - returns number of matches
# - next goes back to start of loop
next if scalar grep( /^$k$/, @aFiles);
print "key=$k\n";
}
For more information, see scalar and perlre
Best, beth
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.