Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

How can one delete an element and it corresponding values from the array of arrays?

by supriyoch_2008 (Monk)
on Mar 18, 2013 at 07:16 UTC ( #1023980=perlquestion: print w/replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I have 3 arrays i.e. @sample1, @sample2 and @sample3 each with 4 elements. One element in one array is linked to the corresponding position element in the other two arrays. I mean to say that a, 1 & x1 are linked and so are the others. Now I want to put all the arrays in a bigger array @big. At some point of time, I might be interested to delete one element x3 from @sample3 and its corresponding values from @sample1 and @sample2 i.e. c & 3 and likewise from array @big. The big array is an array of arrays. I am at my wit's end to find out the resultant big array and other three arrays after removing x3 & its corresponding values from @sample1 & @sample2. I am looking forward to suggestions and guidance from PerlMonks in this matter.

#!/usr/bin/perl use warnings; use strict; my @sample1=qw/a b c d/; my @sample2=qw/1 2 3 4 /; my @sample3=qw/x1 x2 x3 x4/; # Joined array: my @big=(@sample1, @sample2, @sample3); print"\n Original Big Array:\n @big\n"; # To store the array values in a hash: my %h; # Code for removing x3 & its corresponding values from big array??? # and code for modified @sample1, @sample2 & @sample3 ??? $deleted_ele="x3"; ???? print"\n Original big array: @big\n Resultant big array: @mod_big\n Deleted Element: $deleted_ele\n Modified sample1: @mod_sam1\n sample2: @mod_sam2\n sample3: @mod_sam3\n\n"; exit;

Result should look something like:

Original big array: a b c d 1 2 3 4 x1 x2 x3 x4 Resultant big array: a b d 1 2 4 x1 x2 x4 Deleted Element: x3 Modified @sample1: a b d @sample2: 1 2 4 @sample3: x1 x2 x4

Replies are listed 'Best First'.
Re: How can one delete an element and it corresponding values from the array of arrays?
by tobyink (Abbot) on Mar 18, 2013 at 08:00 UTC

    It has already been noted in your previous thread that a set of arrays is a poor choice of data structure for your task. Difficulty of deleting a record is one problem of many that you'll encounter - it's much easier using hashes.

    #!/usr/bin/env perl use v5.12; use Data::Dumper; my %students = ( x1 => { name => "Alice", telephone => 1001, regno => "x1", }, x2 => { name => "Bob", telephone => 1002, regno => "x2", }, x3 => { name => "Carol", telephone => 1003, regno => "x3", }, ); say "Let's look at the data structure..."; print Dumper \%students; say "What is student x1's telephone number?"; say $students{x1}{telephone}; say "What is Carol's telephone number?"; my ($carol) = grep { $_->{name} eq "Carol" } values %students; say $carol->{telephone}; say "Now let's delete student x2..."; delete $students{x2}; say "And change Carol's phone number"; $carol->{telephone} = "1004"; say "Let's look at the data structure again..."; print Dumper \%students;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Hi tobyink

      Thanks for your suggestions. I have noticed that you have mentioned in previous thread that a set of arrays is a poor choice of data structures. Honestly speaking, I find the Data structures in perl (like arrays of arrays, hashes of arrays, arrays of hashes etc.) very complicated for my understanding because of diverse use of parentheses & brackets and because of my recent introduction to perl as a biologist. Perl is the first computer language that I have been trying to learn through personal endeavor. I donot have any formal training on computer and its applications. I hope you will understand my limitations in understanding a computer language. Moreover, it may sound strange that I started learning how to use a computer(a laptop) since 22nd February 2008 when I was given a laptop by the university. Prior to that I could not afford a laptop because of its high cost.


        The nice thing is that there are really only three things to learn, and then the world of data structures is all yours.

        You've got Arrays, and you've got Hashes, and then you've got references. Once you wrap your head around those three, and see the recursive possibilities, you can make pretty much anything.

Re: How can one delete an element and its corresponding values from an array of arrays?
by Athanasius (Chancellor) on Mar 18, 2013 at 08:20 UTC

    Hello again, supriyoch_2008,

    I totally agree with tobyink that a hash is the superior option here. But, if you insist on using a 2-dimensional array, here is how to code it:

    #! perl use strict; use warnings; use Data::Dumper; my @sample1 = qw/ a b c d /; my @sample2 = qw/ 1 2 3 4 /; my @sample3 = qw/ x1 x2 x3 x4 /; my @big = (\@sample1, \@sample2, \@sample3); # <-- Note: use re +ferences here! print "Original big array:\n", Dumper(@big), "\n"; my $deleted_ele = 'x3'; remove(\@big, $deleted_ele); print "Resultant big array:\n", Dumper(@big), "\n"; print "Deleted element: $deleted_ele\n"; print "Modified sample1:\n", Dumper(@sample1), "\n"; print "Modified sample2:\n", Dumper(@sample2), "\n"; print "Modified sample3:\n", Dumper(@sample3); sub remove { my ($array, $to_delete) = @_; for my $i (0 .. @$array - 1) { for my $j (0 .. @{$array->[$i]} - 1) { if ($array->[$i][$j] eq $to_delete) { splice @$array[$_], $j, 1 for 0 .. @$array - 1; # < +-- Can't use delete here return; } } } }

    Compare this code to the solution given by tobyink, and it should be obvious that a hash is the way to go!

    Hope that helps,

    Update: Added 2 comments to the code.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hi Athanasius,

      Thanks for your reply to this thread. With reference to your reply to my previous thread (not this one), is it possible to use print syntax to get the detailed information of a student so that I can get a text output? If yes, where and how should I use print to get the results.


        One of the main benefits of grouping related data together is that it makes accessing that data easier. (The other main benefit is that it becomes harder to corrupt the data by accident.) So, in Re: How can one access all the details of a person using arrays & hash? I created a Student class to hold student data, and I added a method sub display to group together the print statements used to print out the data for a single student.

        Not sure what you’re asking here, but perhaps you want to be able to print to files instead of to STDOUT? If so, just change the calls to print into calls to sprintf and return the string to be printed:

        sub display { my ($self) = @_; my $output = ''; if ($self) { $output = sprintf "Name: %s\n", $self->{NAME}; $output .= sprintf "Age: %d\n", $self->{AGE}; $output .= sprintf "Regd no: %s\n", $self->{REGD_NO}; $output .= sprintf "Phone num: %s\n", $self->{PHONE_NUM}; $output .= sprintf "Mark: %d\n", $self->{MARK}; $output .= sprintf "Pass year: %d\n", $self->{PASS_YEAR}; } return $output; }

        Then print the result:

        my $outfile = 'temp.txt'; open(my $fh, '>', $outfile) or die "Cannot open file '$outfile' for wr +iting: $!"; print $fh find_student('x3')->display(); close($fh) or die "Cannot close file '$outfile': $!";

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: How can one delete an element and it corresponding values from the array of arrays?
by Anonymous Monk on Mar 18, 2013 at 07:29 UTC

      Hi Anonymous Monk,

      Thanks for your valuable suggestions. I shall read those topics once again.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023980]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2018-05-28 05:46 GMT
Find Nodes?
    Voting Booth?