Perl - grep in foreach does not work with $_ but was with variable












0















I want to delete some defined elements from a array. I already have a solution with grep and one element: @big = grep { ! /3/ } @big. In case of several elements I want to put them in an array and using foreach. @big is the array from which I want delete elements from @del:



perl -e "@big = (1,2,3,4,5); @del = (2,4); 
foreach $i (@del) {@big = grep { ! /$i/ } @big; print "@bign"}"


Here is the output:



1 3 4 5
1 3 5


This works fine for me. If I want to use default variable $_ it does not work:



perl -e "@big = (1,2,3,4,5); @del = (2,4); 
foreach (@del) {@big = grep { ! /$_/ } @big; print "@bign"}"


This gives no output. Any idea what happens?










share|improve this question


















  • 2





    foreach and grep both clobber $_. And if you re-assign @big on every iteration, your code probably isn't doing what you want anyway!

    – Matt Jacob
    Nov 22 '18 at 15:45






  • 1





    @MattJacob why not? it deletes one element at every iteration over @del (or more, if more elements are matching). it might not be the fastest solution, but it's correct

    – tinita
    Nov 22 '18 at 17:04













  • @tinita Oops, you're right. That's what I get for browsing SO and trying to cook for Thanksgiving at the same time.

    – Matt Jacob
    Nov 22 '18 at 18:05
















0















I want to delete some defined elements from a array. I already have a solution with grep and one element: @big = grep { ! /3/ } @big. In case of several elements I want to put them in an array and using foreach. @big is the array from which I want delete elements from @del:



perl -e "@big = (1,2,3,4,5); @del = (2,4); 
foreach $i (@del) {@big = grep { ! /$i/ } @big; print "@bign"}"


Here is the output:



1 3 4 5
1 3 5


This works fine for me. If I want to use default variable $_ it does not work:



perl -e "@big = (1,2,3,4,5); @del = (2,4); 
foreach (@del) {@big = grep { ! /$_/ } @big; print "@bign"}"


This gives no output. Any idea what happens?










share|improve this question


















  • 2





    foreach and grep both clobber $_. And if you re-assign @big on every iteration, your code probably isn't doing what you want anyway!

    – Matt Jacob
    Nov 22 '18 at 15:45






  • 1





    @MattJacob why not? it deletes one element at every iteration over @del (or more, if more elements are matching). it might not be the fastest solution, but it's correct

    – tinita
    Nov 22 '18 at 17:04













  • @tinita Oops, you're right. That's what I get for browsing SO and trying to cook for Thanksgiving at the same time.

    – Matt Jacob
    Nov 22 '18 at 18:05














0












0








0








I want to delete some defined elements from a array. I already have a solution with grep and one element: @big = grep { ! /3/ } @big. In case of several elements I want to put them in an array and using foreach. @big is the array from which I want delete elements from @del:



perl -e "@big = (1,2,3,4,5); @del = (2,4); 
foreach $i (@del) {@big = grep { ! /$i/ } @big; print "@bign"}"


Here is the output:



1 3 4 5
1 3 5


This works fine for me. If I want to use default variable $_ it does not work:



perl -e "@big = (1,2,3,4,5); @del = (2,4); 
foreach (@del) {@big = grep { ! /$_/ } @big; print "@bign"}"


This gives no output. Any idea what happens?










share|improve this question














I want to delete some defined elements from a array. I already have a solution with grep and one element: @big = grep { ! /3/ } @big. In case of several elements I want to put them in an array and using foreach. @big is the array from which I want delete elements from @del:



perl -e "@big = (1,2,3,4,5); @del = (2,4); 
foreach $i (@del) {@big = grep { ! /$i/ } @big; print "@bign"}"


Here is the output:



1 3 4 5
1 3 5


This works fine for me. If I want to use default variable $_ it does not work:



perl -e "@big = (1,2,3,4,5); @del = (2,4); 
foreach (@del) {@big = grep { ! /$_/ } @big; print "@bign"}"


This gives no output. Any idea what happens?







arrays perl foreach grep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 15:41









giordanogiordano

1,03821634




1,03821634








  • 2





    foreach and grep both clobber $_. And if you re-assign @big on every iteration, your code probably isn't doing what you want anyway!

    – Matt Jacob
    Nov 22 '18 at 15:45






  • 1





    @MattJacob why not? it deletes one element at every iteration over @del (or more, if more elements are matching). it might not be the fastest solution, but it's correct

    – tinita
    Nov 22 '18 at 17:04













  • @tinita Oops, you're right. That's what I get for browsing SO and trying to cook for Thanksgiving at the same time.

    – Matt Jacob
    Nov 22 '18 at 18:05














  • 2





    foreach and grep both clobber $_. And if you re-assign @big on every iteration, your code probably isn't doing what you want anyway!

    – Matt Jacob
    Nov 22 '18 at 15:45






  • 1





    @MattJacob why not? it deletes one element at every iteration over @del (or more, if more elements are matching). it might not be the fastest solution, but it's correct

    – tinita
    Nov 22 '18 at 17:04













  • @tinita Oops, you're right. That's what I get for browsing SO and trying to cook for Thanksgiving at the same time.

    – Matt Jacob
    Nov 22 '18 at 18:05








2




2





foreach and grep both clobber $_. And if you re-assign @big on every iteration, your code probably isn't doing what you want anyway!

– Matt Jacob
Nov 22 '18 at 15:45





foreach and grep both clobber $_. And if you re-assign @big on every iteration, your code probably isn't doing what you want anyway!

– Matt Jacob
Nov 22 '18 at 15:45




1




1





@MattJacob why not? it deletes one element at every iteration over @del (or more, if more elements are matching). it might not be the fastest solution, but it's correct

– tinita
Nov 22 '18 at 17:04







@MattJacob why not? it deletes one element at every iteration over @del (or more, if more elements are matching). it might not be the fastest solution, but it's correct

– tinita
Nov 22 '18 at 17:04















@tinita Oops, you're right. That's what I get for browsing SO and trying to cook for Thanksgiving at the same time.

– Matt Jacob
Nov 22 '18 at 18:05





@tinita Oops, you're right. That's what I get for browsing SO and trying to cook for Thanksgiving at the same time.

– Matt Jacob
Nov 22 '18 at 18:05












1 Answer
1






active

oldest

votes


















2














As @MattJacob points out, there are two conflicting uses for $_:



foreach (@del) {...}


This implicitly uses $_ as the loop variable. From the foreach docs:




The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.




Your grep command also uses $_, always, as part of its processing. So if you had a value in $_ as part of your foreach, and it was replaced in the grep ...?



From the grep docs:




Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element)




Please do check the grep docs, since there is an explicit warning about modifying a list that you are iterating over. If you are trying to iteratively shrink the list, you might consider some alternative ways to process it. (For example, could you build a single pattern for grep that would combine all the values in your @del list, and just process the @big list one time? This might be quicker, too.)






share|improve this answer
























  • Thanks! If I had a look at the foreach or grep docs I would have avoid to use $_. The advantage to put the question on SO is that you suggest to do this task in another way. What do you mean with build a single pattern for? Is that a regular expression which match all elements of @del? For exampe, $del = '2|4' and then: @big = grep { ! /$del/ } @big;. This is a good idea and so simple. It is embarassing not to get it myself. Interesting would be how to cope if putting in a pattern is not possible.

    – giordano
    Nov 23 '18 at 10:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434323%2fperl-grep-in-foreach-does-not-work-with-but-was-with-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














As @MattJacob points out, there are two conflicting uses for $_:



foreach (@del) {...}


This implicitly uses $_ as the loop variable. From the foreach docs:




The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.




Your grep command also uses $_, always, as part of its processing. So if you had a value in $_ as part of your foreach, and it was replaced in the grep ...?



From the grep docs:




Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element)




Please do check the grep docs, since there is an explicit warning about modifying a list that you are iterating over. If you are trying to iteratively shrink the list, you might consider some alternative ways to process it. (For example, could you build a single pattern for grep that would combine all the values in your @del list, and just process the @big list one time? This might be quicker, too.)






share|improve this answer
























  • Thanks! If I had a look at the foreach or grep docs I would have avoid to use $_. The advantage to put the question on SO is that you suggest to do this task in another way. What do you mean with build a single pattern for? Is that a regular expression which match all elements of @del? For exampe, $del = '2|4' and then: @big = grep { ! /$del/ } @big;. This is a good idea and so simple. It is embarassing not to get it myself. Interesting would be how to cope if putting in a pattern is not possible.

    – giordano
    Nov 23 '18 at 10:21
















2














As @MattJacob points out, there are two conflicting uses for $_:



foreach (@del) {...}


This implicitly uses $_ as the loop variable. From the foreach docs:




The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.




Your grep command also uses $_, always, as part of its processing. So if you had a value in $_ as part of your foreach, and it was replaced in the grep ...?



From the grep docs:




Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element)




Please do check the grep docs, since there is an explicit warning about modifying a list that you are iterating over. If you are trying to iteratively shrink the list, you might consider some alternative ways to process it. (For example, could you build a single pattern for grep that would combine all the values in your @del list, and just process the @big list one time? This might be quicker, too.)






share|improve this answer
























  • Thanks! If I had a look at the foreach or grep docs I would have avoid to use $_. The advantage to put the question on SO is that you suggest to do this task in another way. What do you mean with build a single pattern for? Is that a regular expression which match all elements of @del? For exampe, $del = '2|4' and then: @big = grep { ! /$del/ } @big;. This is a good idea and so simple. It is embarassing not to get it myself. Interesting would be how to cope if putting in a pattern is not possible.

    – giordano
    Nov 23 '18 at 10:21














2












2








2







As @MattJacob points out, there are two conflicting uses for $_:



foreach (@del) {...}


This implicitly uses $_ as the loop variable. From the foreach docs:




The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.




Your grep command also uses $_, always, as part of its processing. So if you had a value in $_ as part of your foreach, and it was replaced in the grep ...?



From the grep docs:




Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element)




Please do check the grep docs, since there is an explicit warning about modifying a list that you are iterating over. If you are trying to iteratively shrink the list, you might consider some alternative ways to process it. (For example, could you build a single pattern for grep that would combine all the values in your @del list, and just process the @big list one time? This might be quicker, too.)






share|improve this answer













As @MattJacob points out, there are two conflicting uses for $_:



foreach (@del) {...}


This implicitly uses $_ as the loop variable. From the foreach docs:




The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.




Your grep command also uses $_, always, as part of its processing. So if you had a value in $_ as part of your foreach, and it was replaced in the grep ...?



From the grep docs:




Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element)




Please do check the grep docs, since there is an explicit warning about modifying a list that you are iterating over. If you are trying to iteratively shrink the list, you might consider some alternative ways to process it. (For example, could you build a single pattern for grep that would combine all the values in your @del list, and just process the @big list one time? This might be quicker, too.)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 15:57









aghastaghast

11.2k11035




11.2k11035













  • Thanks! If I had a look at the foreach or grep docs I would have avoid to use $_. The advantage to put the question on SO is that you suggest to do this task in another way. What do you mean with build a single pattern for? Is that a regular expression which match all elements of @del? For exampe, $del = '2|4' and then: @big = grep { ! /$del/ } @big;. This is a good idea and so simple. It is embarassing not to get it myself. Interesting would be how to cope if putting in a pattern is not possible.

    – giordano
    Nov 23 '18 at 10:21



















  • Thanks! If I had a look at the foreach or grep docs I would have avoid to use $_. The advantage to put the question on SO is that you suggest to do this task in another way. What do you mean with build a single pattern for? Is that a regular expression which match all elements of @del? For exampe, $del = '2|4' and then: @big = grep { ! /$del/ } @big;. This is a good idea and so simple. It is embarassing not to get it myself. Interesting would be how to cope if putting in a pattern is not possible.

    – giordano
    Nov 23 '18 at 10:21

















Thanks! If I had a look at the foreach or grep docs I would have avoid to use $_. The advantage to put the question on SO is that you suggest to do this task in another way. What do you mean with build a single pattern for? Is that a regular expression which match all elements of @del? For exampe, $del = '2|4' and then: @big = grep { ! /$del/ } @big;. This is a good idea and so simple. It is embarassing not to get it myself. Interesting would be how to cope if putting in a pattern is not possible.

– giordano
Nov 23 '18 at 10:21





Thanks! If I had a look at the foreach or grep docs I would have avoid to use $_. The advantage to put the question on SO is that you suggest to do this task in another way. What do you mean with build a single pattern for? Is that a regular expression which match all elements of @del? For exampe, $del = '2|4' and then: @big = grep { ! /$del/ } @big;. This is a good idea and so simple. It is embarassing not to get it myself. Interesting would be how to cope if putting in a pattern is not possible.

– giordano
Nov 23 '18 at 10:21




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434323%2fperl-grep-in-foreach-does-not-work-with-but-was-with-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]