|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Notations: Grep { /.../ } @array -vs- grep(/.../, @array)
I recall reading from an external source that the notation grep { /.../ } @array offers advantages over the other notation grep(/.../, @array). But I can't find the article any more. Does anyone have anything to say about that?
The only thing I can think of is if you can incorporate some complex expressions in the { } that you can't incorporate into the ( ). |
|
#2
|
||||
|
||||
|
Nope. { } is actually a code block, so you can put more complex expressions that cannot be evaluated in a single expression.
Basically grep has two forms: grep BLOCK LIST grep EXPR, LIST Note that there is no comma between BLOCK and LIST. For EXPR, you would normally use a simple expression such as: Code:
@array = (1, 2, -1, 3); @array2 = grep $_ > 1, @array; This will pull all elements of @array that are > 1 (i.e.) 2 and 3. By the way, EXPR does not need to be a regular expression either, it can be a simple expression of any kind. This can also be easily implemented as a block with virtually the same syntax: Code:
@array = (1, 2, -1, 3);
@array2 = grep {$_ > 1} @array;
Now if you have something more complex that cannot be achieved in a single expression. Say you have to map some values to some other values according to the following rules: value > 0 and value <= 2: group 1 value > 2 and value <= 5: group 2 value > 5 and value <= 8: group 3 anything else : group 4 To do this mapping with a grep, you would use a block like this: Code:
@array = (1, 5, 2, 6, 10, 4);
@array2 = grep { if ($_ > 0 && $_ <= 2) {
$_ = 1;
}
elsif ($_ > 2 && $_ <= 5) {
$_ = 2;
}
elsif ($_ > 5 && $_ <= 8) {
$_ = 3;
}
else {
$_ = 4;
}
} @array;
You cannot do this sort of logic with a simple expression.
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#3
|
||||
|
||||
|
I see the point you're making Scorp, but surely the last example is better suited to 'map', so you can create @array2 without altering the values in @array.
__________________
~ishnid; Have you tried: [ search.cpan.org | perldoc | Java API | mysql.com | google ] Apostrophes are NOT used for possessive pronouns or for noun plurals, including acronyms. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Notations: Grep { /.../ } @array -vs- grep(/.../, @array) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|