Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

splitting a list

by ChuckularOne (Parson)
on Jul 05, 2005 at 15:32 UTC ( #472486=perlquestion: print w/ replies, xml ) Need Help??
ChuckularOne has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to split a list.
I have a 2 lists and I need to output the contents of list1 and the contents of list2 that are not in list1.

List1:List2
educationeducation
productionjoe
resultsbob
 carl
 production
 steve
 results
 test

So I have the following output:
OutList1:OutList2
educationjoe
productionbob
resultscarl
 steve
 test

Thanks in advance.

I tried to search for this in the archives, but I have no clue how to phrase the search. If someone can simply help me with the search, that will be more than enough help.

Comment on splitting a list
Re: splitting a list
by Tanktalus (Canon) on Jul 05, 2005 at 15:41 UTC

    Check the FAQs for "list" - "perldoc -q list" can help.

    my @l1 = qw(education production results); my @l2 = qw(education joe bob carl production steve results tests); my %h1; undef @h1{@l1}; my @o2 = grep { not exists $h1{$_} } @l2;
      Bad (undocumented behavior):
      undef @h1{@l1}
      Better:
      @h1{@l1} = ();

      Caution: Contents may have been coded under pressure.
Re: splitting a list
by Roy Johnson (Monsignor) on Jul 05, 2005 at 15:42 UTC
    perldoc -q duplicate

    Caution: Contents may have been coded under pressure.
Re: splitting a list
by TedPride (Priest) on Jul 05, 2005 at 15:55 UTC
    use strict; use warnings; my @list1 = qw/education production results/; my @list2 = qw/education joe bob carl production steve results test/; print join "\n", notIn(\@list2, \@list1); sub notIn { my (@arr, %hash); @hash{@{$_[1]}} = (); for (@{$_[0]}) { push @arr, $_ if !exists $hash{$_}; } return @arr; }
Re: splitting a list
by dirac (Beadle) on Jul 05, 2005 at 15:56 UTC
    #!/usr/bin/perl -w
    use strict;
    
    my %set;
    my @list1 = qw(education production results);
    my @list2 = qw(education joe bob carl production steve results tests);
    @set{@list2} = @list2;
    delete @set{@list1};
    print map {"$_\n"} keys %set;
    
Re: splitting a list
by radiantmatrix (Parson) on Jul 05, 2005 at 16:09 UTC

    I would, very simply, map List2 into a hash and compare that hash to List1, storing the results. As your problem is specified, there is no need to modify list 1.

    use strict; my @list1 = qw(education production results); my @list2 = qw(education joe bob carl production steve results test); my %mapping = map { $_ => undef } @list1; my (@l2_only); undef @l2_only; foreach (@list2) { push(@l2_only, $_) unless exists $mapping{$_}; } print 'List 1: ', join(', ', @list1),"\n"; print 'List 2: ', join(', ', @l2_only), "\n"; __END__ The script above prints: List 1: education, production, results List 2: joe, bob, carl, steve, test
    Larry Wall is Yoda: there is no try{}
    The Code that can be seen is not the true Code
Re: splitting a list
by dorko (Parson) on Jul 05, 2005 at 18:43 UTC
    Be lazy, stand on the shoulders of others. List::Compare does all that and more. (The code below has been mildly tested.)
    #!/usr/bin/perl use strict; use warnings; use diagnostics; use List::Compare; my @List1 = qw(education production results); my @List2 = qw(education joe bob carl production steve results test); my @OutList1 = @List1; #Part I is done... my $lc = List::Compare->new(\@List1, \@List2); # Get those items which appear (at least once) only in the second list +. my @OutList2 = $lc->get_complement; my $n; foreach (@OutList2) {print $n++.": $_\n"};
    Output:
    0: bob 1: carl 2: joe 3: steve 4: test

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: splitting a list
by tlm (Prior) on Jul 06, 2005 at 01:54 UTC

    Against my doctors orders, I continue to use:

    my @no_dups = do { my %h; grep !$h{ $_ }++, @list1, @list2 };

    the lowliest monk

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2015-07-06 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls