Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found