Re: commify_series script in cookbook page 94



yitzle wrote:
my $sepchar = grep( /,/ => @_ ) ? ";" : ",";

And I also don't understand what ";" is doing in the ternary operator??
The ";" is what is returned if the condition is TRUE.

"( / ,/ => @_)" is a(n anonymous) hash.

No it is not. /,/ and @_ are just two arguments to the grep() function.


I've had no idea that grep can
operate on a hash.
However, it seems that "grep( / ,/ => @_)" and "grep( / ,/, @_)" are equivalent.

Yes, '=>' and ',' are just different versions of the comma operator.


__CODE__
@a = qw/aa ab ba bb/;
print join(", ", grep(/a/, @a) );
print "\n";
print join(", ", grep(/a/ => @a) );
print "\n";
__END__
aa, ab, ba
aa, ab, ba

@_ is a list/array, and that is what grep is searching for the RegEx / ,/.

perldoc -q "What is the difference between a list and an array"

And the regexp in the example is /,/ not / ,/.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: "index" efficiency... any help or ideas?
    ... > dealing with different searching algorithms. ... > cons of hashing vs. binary searching. ... That's the killer reason for using hashes in this application. ... Searching a hash table is ...
    (alt.lang.asm)
  • Re: further explanation re: FAQ 4.41 How can I remove duplicate elements from a list or array?
    ... The '%' indicates that %saw is a hash. ... Hashes are also known as "associative arrays". ... you have to understand *what* grep() is checking for trueness. ... push the current address to ...
    (comp.lang.perl.misc)
  • Re: "Escape" in perl
    ... Computers are much better at searching than using paper... ... I never actually use perldoc for searching the standard docs. ... that I can then grep it). ... my "Perl problem resolution checklist", ...
    (comp.lang.perl.misc)
  • A hash or array of regexps?
    ... I often find myself with a list of things that I'm searching for. ... I build a hash with the ... key being the four-character pattern, ... as quick or efficient as simple keys. ...
    (comp.lang.perl.misc)
  • Re: Searching for Link List Implementation (with requirements)
    ... sorting, searching, insert and deleting by key? ... of many {unsigned int process id (pid), ... Prior I had been using a hash table (taken ...
    (comp.lang.c)