Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Create a list from an Arrray

by WorkerBee (Novice)
on Jul 16, 2013 at 12:40 UTC ( #1044582=perlquestion: print w/replies, xml ) Need Help??
WorkerBee has asked for the wisdom of the Perl Monks concerning the following question:

Hello again, Monks! I am trying to create a list from an array and cannot seem to get it... I am trying compare to arrays into lists so that I can compare the lists to determine the intersection. However, the syntax I am using is not expanding into a list. Thank you, Monks! Bee
@USERlist = qw(@hash_tags); print "USERTAGS:@USERlist"; @STATIClist = qw(@tags); $lc = List::Compare->new(\@USERlist, \@STATIClist); @valid_tag = $lc->get_intersection; print "VALID_TAG:@valid_tag";

Replies are listed 'Best First'.
Re: Create a list from an Array
by Athanasius (Chancellor) on Jul 16, 2013 at 12:55 UTC
Re: Create a list from an Arrray
by rjt (Deacon) on Jul 16, 2013 at 13:02 UTC
    @USERlist = qw(@hash_tags);

    See qw: The above expression won't interpolate the array. In order to take a reference to @hash_tags, you already have the correct syntax:

            $lc = List::Compare->new(\@USERlist, \@STATIClist);

    Just replace \@USERlist, \@STATIClist with \@hash_tags, \@tags. Since you're not modifying the original arrays, there is no need to make a copy, so just put the original arrays in your print statements as well, such as:

    print "USERTAGS:@hash_tags";
Re: Create a list from an Arrray
by davido (Archbishop) on Jul 16, 2013 at 15:43 UTC

    You want to know which entries are in both lists:

    my %matched; @matched{@STATIClist} = (); print "Valid tags:\n"; print "\t$_\n" for grep { exists $matched{$_} } @USERlist;

    If you want to store the list...:

    my @valid_tag = grep { exists $matched{$_} } @USERlist;


      What about good old hash functions?

      use Data::Dumper my(@list_a)=(qw(a b c d e f g)); my(@list_b)=(qw(a c e g h j k)); my(%just_a,%just_b,%both); @just_a{@list_a}=(); delete @just_a{@list_b}; @just_b{@list_b}=(); delete @just_b{@list_a}; @both{@list_a}=(); delete @both{keys %just_a}; print Data::Dumper->Dump([ [keys %just_a],[keys %just_b],[keys %both] ], [qw/just_a just_b both/] )
      $just_a = [ 'b', 'd', 'f' ]; $just_b = [ 'k', 'h', 'j' ]; $both = [ 'e', 'c', 'a', 'g' ];

Re: Create a list from an Arrray
by ww (Archbishop) on Jul 16, 2013 at 13:23 UTC
    This is a lazy question (where 'lazy' ne 'virtue').

    Mis-interpreted question. However, THERE IS A BETTER WAY:

    Search for an answer -- that's probably going to be quicker than asking and waiting for an answer. This is almost a FAQ and definitely can be found with minimal effort on this site.

    Super Search or Google intersection arrays will offer ways to solve your problem without annoying the electrons and silicon by copying the arrays to lists.

    If I've misconstrued your question or the logic needed to answer it, my apologies to all those electrons which were inconvenienced by the creation of this post.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1044582]
Approved by hdb
[shmem]: just one proverb before dozing off: when dealing with a bear, hubris is suicidal
[shmem]: Old_Gray_Bear would nod, and go back to sleep, then - but he's been sleeping for a year now.
[erix]: "AngloZionist Empire"? "Stop the Empire's war on Russia?" WTF?
erix stops reading as recommended by the blog itself
[erix]: my attention being jealously guarded ;)
[shmem]: erix, yes, it's a WTF. Since...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2018-03-19 20:49 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.