Re: Need help with removing values from arrays
by kcott (Archbishop) on Aug 15, 2017 at 05:27 UTC
|
G'day GrizzlyRizly,
Welcome to the Monastery.
That sounds like you're using one of the many GUIs available in Perl: you need to state which.
These GUIs can have multiple combobox widgets: again, you need to state which.
When a combobox item is selected, GUIs can provide various types of information about the selection,
such as an index or a string.
If you have an index, and don't care about preserving the original array,
perhaps use splice.
$ perl -E 'my @x = qw{a b c}; say "@x"; splice @x, 1, 1; say "@x"'
a b c
a c
If you have an index, but do care about preserving the original array,
perhaps use a slice.
$ perl -E 'my @x = qw{a b c}; say "@x"; my @y = @x[0,2]; say "@y"'
a b c
a c
If you have the selection as a string, and do care about preserving the original array,
perhaps create a new array using grep.
$ perl -E 'my @x = qw{a b c}; say "@x"; my $sel = "b"; my @y = grep {
+$_ ne $sel } @x; say "@y"'
a b c
a c
There's other ways to do this depending on your requirements.
You should really show us some sample code (see SSCCE):
we might be able to suggest a completely different approach when we have a better idea what you're currently doing.
Also see the guidelines in "How do I post a question effectively?" for tips on the type of information to provide us with.
| [reply] [d/l] [select] |
|
Hi, i have updated the information given, please tell me if i need to give anymore information and i will gladly do so, thanks.
| [reply] |
|
When you update a post, it is important to indicate what you have changed. You should be careful not to invalidate any existing reply which refers to the original post.
| [reply] |
|
| [reply] [d/l] |
Re: Need help with removing values from arrays
by AnomalousMonk (Archbishop) on Aug 15, 2017 at 05:05 UTC
|
Can you please provide us with a Short, Self-Contained, Correct Example? Otherwise, we're left trying to reverse engineer an application from the rather vague description in your OP. Few monks will be willing to invest much time in what seems likely to be a futile guessing game. Please help us to help you.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] |
|
| [reply] |
|
Okay, I had a few minutes, so I added all the mystatements and provided some initializers to get around the error messages.
Your code still doesn't do anything:
#!/usr/bin/perl
use strict;
use warnings;
my $str = "1,2,3,4,5";
my @array = ();
my ($ids, $attri) = split /,/, $str;
print " \$ids: [$ids]\n";
print " \$attri: [$attri]\n";
splice (@array,$ids, 1);
print " \@array:\n";
my $array_index = 0;
foreach my $array_element (@array) {
print " [$array_index]: [$array_element]\n";
}
print "Okay, now what?\n";
sub refreshdisplay{
my $id=0;
my $arraylist = "";
for($id=0;$id<=$#array;$id++){
if($id == 0){
$arraylist = "$array[0]";
}
else{
$arraylist .= "$id,$array[$id]\n";
}
}
}
Yields:
S:\Steve\Dev\PerlMonks\P-2017-08-15@0704-combobox>perl combobox0b.pl
$ids: [1]
$attri: [2]
splice() offset past end of array at combobox0b.pl line 12.
@array:
Okay, now what?
S:\Steve\Dev\PerlMonks\P-2017-08-15@0704-combobox>
| [reply] [d/l] [select] |
|
|
|
| [reply] [d/l] |
Re: Need help with removing values from arrays
by zentara (Archbishop) on Aug 15, 2017 at 16:04 UTC
|
within the combobox for the gui that i am using im not entirely sure about it, but when i use the program to create it i select the perl TKx option
I think you should post your Tkx code first, because the GUI toolkit widgets have a few methods for handling array sets, which is probably what you are after. What is it you want your GUI to do? Remove elements from the list?
| [reply] |
Re: Need help with removing values from arrays
by wjw (Priest) on Aug 15, 2017 at 16:13 UTC
|
Based on what you are doing in the code you posted, I would think about using two arrays. One to hold all the original values, and one to hold what you want displayed in the combobox. Once you have received the value to be deleted, simply generate the second array by parsing through the IDs of the first array and add them to the second unless the ID matches the one selected in the first combobox.
There are plenty of other options, but I based this on what you show your are attempting to do in your posted code. I have a notion that there is a module somewhere on CPAN which will make this easier for you, but without knowing more about what and how you are doing things, it is a bit hard to suggest options.
...the majority is always wrong, and always the last to know about it...
A solution is nothing more than a clearly stated problem...
| [reply] |
Re: Need help with removing values from arrays
by dbander (Scribe) on Aug 15, 2017 at 17:26 UTC
|
$ cat ./arrayreducto.pl
#!/usr/bin/perl
use strict;
use warnings;
my $old_combo = "USA,Canada,Australia";
print "Old Combobox: $old_combo\n";
my $str = 'Canada';
print "Selected: $str\n";
my @old_combolist = split /\,/, $old_combo;
my $str_regex = quotemeta $str;
my @new_combolist = ();
foreach my $old_comboitem (@old_combolist) {
# Must match end to end but user may have entered in wrong case
if ($old_comboitem !~ /^$str_regex$/i) {
push @new_combolist, $old_comboitem;
}
}
my $new_combo = join ',', @new_combolist;
print "New Combobox: $new_combo\n";
$ perl ./arrayreducto.pl
Old Combobox: USA,Canada,Australia
Selected: Canada
New Combobox: USA,Australia
| [reply] [d/l] |