g_speran has asked for the wisdom of the Perl Monks concerning the following question:
Hello Perl Monks, I'm sure one you see what I am doing you may be like "What in the BLEEP, are or doing or thinking". Well, this is why I am reaching out to the Monks, as i believe there has to be a better way to do this. I am trying to locate all the element in the @ARGV that were passed via command line and ONLY delete some of them. in the example, I want to delete --config and --enter Now this is only a snippet of the code The results I get are now expected, but there has to be a much simpler way
> splice_tst.pl --debug --config --enter --config --config --enter +--help
use strict; Splice_Array("--config"); Splice_Array("--enter"); sub Splice_Array { print "Before Array Splicing: \n"; print "\t" . join(',', @ARGV), "\n"; my $value=shift; print "Splicing Value: $value\n"; my @matches = grep { $ARGV[$_] eq $value} 0..$#ARGV; # Just to Cou +nt the number of times the variable was identified print "$value is located at index(s): " . join(',', @matches), "\n +"; for my $match (@matches) { # just used for a Max loop count my @index = grep { $ARGV[$_] eq $value} 0..$#ARGV; print "Index:$index[0]\n"; splice @ARGV, $index[0] ,1; } print "After Array Splicing: \n"; print "\t" . join(',', @ARGV), "\n\n\n"; }
Output: C:\Temp>splice_tst.pl --debug --config --enter --config --config --ent +er --help Before Array Splicing: --debug,--config,--enter,--config,--config,--enter,--help Splicing Value: --config --config is located at index(s): 1,3,4 Index:1 Index:2 Index:2 After Array Splicing: --debug,--enter,--enter,--help Before Array Splicing: --debug,--enter,--enter,--help Splicing Value: --enter --enter is located at index(s): 1,2 Index:1 Index:1 After Array Splicing: --debug,--help C:\Temp>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Locate emement in array and delete
by NERDVANA (Priest) on Aug 25, 2023 at 19:51 UTC | |
|
Re: Locate emement in array and delete
by choroba (Cardinal) on Aug 25, 2023 at 19:30 UTC | |
|
Re: Locate emement in array and delete
by Anonymous Monk on Aug 26, 2023 at 12:44 UTC |