Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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";

Comment on Create a list from an Arrray
Download Code
Re: Create a list from an Array
by Athanasius (Prior) 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 ww (Bishop) 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 site:perlmonks.org 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.
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;

    Dave

      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/] )
      Output:
      $just_a = [ 'b', 'd', 'f' ]; $just_b = [ 'k', 'h', 'j' ]; $both = [ 'e', 'c', 'a', 'g' ];

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-07-11 20:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (235 votes), past polls