Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Need help with removing values from arrays

by GrizzlyRizly (Novice)
on Aug 15, 2017 at 04:23 UTC ( #1197398=perlquestion: print w/replies, xml ) Need Help??

GrizzlyRizly has asked for the wisdom of the Perl Monks concerning the following question:

Hi, so i need help to remove a selected value from an array. i am using a combobox to display all the values in the array as different selections ans once a selection is chosen a button will then be pressed to disable the combobox and enable the next combobox that will then show all of the values of the same array with the value selected from the previous combobox removed from the array. i have been trying to get this to work for a fair while now and nothing i have really done seems to work, i'm looking for either full help or if there are some commands that someone could point me towards that would work, that would be much appreciated.

this is some things i have been trying, i have also been trying by appending a listing ID onto the array, creating a 2D array.

my ($ids, $attri) = split /,/, $str; splice (@array,$ids, 1); sub refreshdisplay{ $id=0; $arraylist = ""; for($id=0;$id<=$#array;$id++){ if($id == 0){ $arraylist = "$array[0]"; } else{ $arraylist .= "$id,$array[$id]\n"; } } }

$str is the variable name for what is selected 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

Replies are listed 'Best First'.
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.

    — Ken

      Hi, i have updated the information given, please tell me if i need to give anymore information and i will gladly do so, thanks.

        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.
        Bill
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:  <%-{-{-{-<

      Hi, i have updated the information given, i hope it is enough but if you need anything more i can keep trying to add as much as i can.

        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>

        You have not given us a complete working example:

        S:\Steve\Dev\PerlMonks\P-2017-08-15@0704-combobox>perl combobox0a.pl Global symbol "$str" requires explicit package name at combobox0a.pl l +ine 5. Global symbol "@array" requires explicit package name at combobox0a.pl + line 6. Global symbol "$id" requires explicit package name at combobox0a.pl li +ne 9. Global symbol "$arraylist" requires explicit package name at combobox0 +a.pl line 10. Global symbol "$id" requires explicit package name at combobox0a.pl li +ne 11. Global symbol "$id" requires explicit package name at combobox0a.pl li +ne 11. Global symbol "@array" requires explicit package name at combobox0a.pl + line 11. Global symbol "$id" requires explicit package name at combobox0a.pl li +ne 11. Global symbol "$id" requires explicit package name at combobox0a.pl li +ne 12. Global symbol "$arraylist" requires explicit package name at combobox0 +a.pl line 13. Global symbol "@array" requires explicit package name at combobox0a.pl + line 13. Global symbol "$arraylist" requires explicit package name at combobox0 +a.pl line 16. Global symbol "$id" requires explicit package name at combobox0a.pl li +ne 16. Global symbol "@array" requires explicit package name at combobox0a.pl + line 16. Global symbol "$id" requires explicit package name at combobox0a.pl li +ne 16. Execution of combobox0a.pl aborted due to compilation errors. S:\Steve\Dev\PerlMonks\P-2017-08-15@0704-combobox>

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?


    I'm not really a human, but I play one on earth. ..... an animated JAPH
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...

Re: Need help with removing values from arrays
by dbander (Scribe) on Aug 15, 2017 at 17:26 UTC

    Changed my answer. This one:

    • Ensures end-to-end match
    • Allows case-insensitive (ComboBox, user might enter wrong case?)
    • Breaks the steps up so it's easier to read

    $ 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1197398]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-12-07 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?