Treating your code as some sort of sentient entity which "refuses to work" will get you nowhere. Your code is no doubt doing exactly what you asked it to do; the problem will be what you asked.

If you do not show your code, along with its output which may consist solely of some error message, we cannot help you! Please read "How do I post a question effectively?" and SSCCE; then provide us with something that we can use to help you.

Your requirements have changed. You now additionally want "... and cat it to the end of the array."; however, you don't say what happens if the element to be removed doesn't exist.

There are a lot posts involving regexes. Based on what you've told us so far, I see no need for the overhead of firing up the regex engine.

I originally provided:

$ perl -E 'my @x = qw{A B C D}; @x = grep $_ ne "B", @x; say "@x"' A C D

With your new additional requirement, you could code:

$ perl -E 'my @x = qw{A B C D}; my $y = "B"; @x = (grep($_ ne $y, @x), + $y); say "@x"' A C D B

If "B" is not found, you perhaps want

$ perl -E 'my @x = qw{A B C D}; my $y = "Z"; @x = (grep($_ ne $y, @x), + $y); say "@x"' A B C D Z

or

$ perl -E 'my @x = qw{A B C D}; my $y = "Z"; @x = (grep($_ ne $y, @x), + $y)[0..$#x]; say "@x"' A B C D

Which still works if "B" is found:

$ perl -E 'my @x = qw{A B C D}; my $y = "B"; @x = (grep($_ ne $y, @x), + $y)[0..$#x]; say "@x"' A C D B

Because these methods simply use string inequality (ne) you won't encounter problems with characters being special to a regular expression:

$ perl -E 'my @history = qw{Cache::SizeAwareMemoryCache(3) dhcp-option +s(5) BN_add_word(3) audit-packages(8)}; my $choice = "dhcp-options(5) +"; @history = (grep($_ ne $choice, @history), $choice); say "@history +"' Cache::SizeAwareMemoryCache(3) BN_add_word(3) audit-packages(8) dhcp-o +ptions(5) $ perl -E 'my @history = qw{Cache::SizeAwareMemoryCache(3) dhcp-option +s(5) BN_add_word(3) audit-packages(8)}; my $choice = "not-found(0)"; +@history = (grep($_ ne $choice, @history), $choice); say "@history"' Cache::SizeAwareMemoryCache(3) dhcp-options(5) BN_add_word(3) audit-pa +ckages(8) not-found(0) $ perl -E 'my @history = qw{Cache::SizeAwareMemoryCache(3) dhcp-option +s(5) BN_add_word(3) audit-packages(8)}; my $choice = "dhcp-options(5) +"; @history = (grep($_ ne $choice, @history), $choice)[0..$#history]; + say "@history"' Cache::SizeAwareMemoryCache(3) BN_add_word(3) audit-packages(8) dhcp-o +ptions(5) $ perl -E 'my @history = qw{Cache::SizeAwareMemoryCache(3) dhcp-option +s(5) BN_add_word(3) audit-packages(8)}; my $choice = "not-found(0)"; +@history = (grep($_ ne $choice, @history), $choice)[0..$#history]; sa +y "@history"' Cache::SizeAwareMemoryCache(3) dhcp-options(5) BN_add_word(3) audit-pa +ckages(8)

— Ken


In reply to Re^3: Removing a string-element from an array of strings by kcott
in thread Removing a string-element from an array of strings by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.