Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Filtering array based off of two values against second array

by Speed_Freak (Sexton)
on Oct 26, 2017 at 04:11 UTC ( [id://1202045]=perlquestion: print w/replies, xml ) Need Help??

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

Alright, I'm working in a few hundred lines of code here that would take exponentially longer to sanitize and create data sets to provide a functional snippet....so I'm not going to. I think the question is easy enough to pose, so I'm hoping I don't get too many "follow all the rules" replies.

I have two arrays. Array one is the finished product of the script.....except it has a ton of extraneous data that I don't want, and has proven incredibly hard to prevent from being entered into the array. Enter array two, my filter.

Array_1 [0] = master_id [1] = sub_id1 [2] = sub_id2 [3] = sub_id3 [4] = sub_id4 Array_2 [0] = data [1] = sub_id

What I need to do is take Array 1 and recreate it for [ 0 ],[ 1 ],[ 2 ],[ 3 ],[ 4 ] for each entry where [ 1 ] or [ 3 ] matches any of the [ 1 ]'s in Array 2

is that straightforward enough? Sorry for the crap post, I'm on a time crunch and traveling away from my notes on this script. Brain is fried.

OOOOH, and while we're at it, lets add a [ 5 ] and [ 6 ] to array 1 that stores any matching [ 0 ] from array 2 for that sub_id!

final filtered output if sub_id1 or sub_id3 exist in array 2: Array_3 [0] = master_id [1] = sub_id1 [2] = sub_id2 [3] = sub_id3 [4] = sub_id4 [5] = sub_id1_data [6] = sub_id2_data

Replies are listed 'Best First'.
Re: Filtering array based off of two values against second array
by haukex (Archbishop) on Oct 26, 2017 at 05:53 UTC
    for each entry where [ 1 ] or [ 3 ] matches any of the [ 1 ]'s in Array 2

    Sorry, but even after re-reading your post twice, this part of the explanation doesn't make sense to me, what is this "[ 1 ] or [ 3 ]" - why should sub_id only match indicies 1 and 3, and/or why does your sample output include all of Array_1 plus the entries sub_id1_data and sub_id2_data (note the emphasis)? And what does "any of the [ 1 ]'s" mean, I see only one entry at each index 1? If you could explain more - best of course with example input, runnable code, and expected output for that input - we could be of more assistance.

    For now, I can only guess that you might want to build a regex out of Array_2 and grep Array_1 with that regex, and for the second part of your question pushing the results back onto Array_1.

    provide a functional snippet....so I'm not going to. I think the question is easy enough to pose, so I'm hoping I don't get too many "follow all the rules" replies.

    The "rules"* are not there just for the sake of being rules or because we like being picky (although that often comes with the territory in programming ;-) ), but because experience has shown it's the most straightforward way to see the problem and provide help, and sorry but your post is another example of that - see my questions above, for example, if the confusion results from typos in the indicies, runnable code would have caught that. So: I know what I mean. Why don't you? How do I post a question effectively? and Short, Self-Contained, Correct Example.

    * Update: Don't think of them as "rules", but as advice to help post good questions. Good questions are not good because they followed all the rules, but because they efficiently provide all the information needed to get good answers. It may be a cliche but in this case it's extremely fitting: Help us help you. (We're all here voluntarily.) Plus, the process of crafting a good question itself will often bring you closer to the answer, since for example cutting your code down to an SSCCE is the same as one would do when debugging anyway.

      Help us help you.
      :-)


      holli

      You can lead your users to water, but alas, you cannot drown them.
Re: Filtering array based off of two values against second array
by kcott (Archbishop) on Oct 26, 2017 at 07:22 UTC

    G'day Speed_Freak,

    "Sorry for the crap post, I'm on a time crunch ..."

    Well, I expect you are. You wasted (your limited) time writing this (in your own words) "crap post" and are no closer to a solution. The aphorism, "more haste, less speed", would seem appropriate here.

    What you've posted, seems to be asking how to do something like these:

    $ perl -E 'my @x = (0..4); my @y = (0,1); my @z; @z = @x if $x[1] == $ +y[1] || $x[3] == $y[1]; say "|@z|"' |0 1 2 3 4| $ perl -E 'my @x = (10..14); my @y = (0,1); my @z; @z = @x if $x[1] == + $y[1] || $x[3] == $y[1]; say "|@z|"' ||

    Or perhaps these:

    $ perl -E 'my @x = (0..4); my @y = (0,1); my @z; @z = @x if $x[1] =~ / +$y[1]/ || $x[3] =~ /$y[1]/; say "|@z|"' |0 1 2 3 4| $ perl -E 'my @x = (10..14); my @y = (0,1); my @z; @z = @x if $x[1] =~ + /$y[1]/ || $x[3] =~ /$y[1]/; say "|@z|"' |10 11 12 13 14|

    Although, I rather suspect that what you really want help with is quite different.

    "... it has a ton of extraneous data that I don't want, and has proven incredibly hard to prevent from being entered into the array."

    It sounds like that is probably what you should be asking about. Fix the source of the problem instead of patching whatever mess comes out the other end.

    "OOOOH, and while we're at it, lets ..."

    Let's not! If it's at all possible, that makes even less sense that what went before it. I read it a few times ... I gave up.

    — Ken

Re: Filtering array based off of two values against second array
by Laurent_R (Canon) on Oct 26, 2017 at 13:29 UTC
    If I understand correctly, you want to keep in Array 1 lines whose identifiers are also in Array 2.

    If so, then start by reading Array 2 and store the (common) identifiers into a hash (as hash keys, values can be anything). Once this is done, read Array 1 and print out the lines whose identifier can be found in the hash previously populated.

    I won't suggest any code, though, because I can't make sense from the input data samples you've provided.

Re: Filtering array based off of two values against second array
by holli (Abbot) on Oct 26, 2017 at 05:18 UTC
    You're not going to post a small, self-contained example? OK. I'm not going to post an answer.


    holli

    You can lead your users to water, but alas, you cannot drown them.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (None)
    As of 2024-04-25 00:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found