Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Print Uniq

by ArifS (Beadle)
on Jul 11, 2016 at 15:02 UTC ( [id://1167558]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that groups bunch of lines and prints in paragraphs. I am trying to remove duplicates while printing those paragraphs. Here is my code-

Original code that prints duplicate paragraphs while called-
for my $list (@lists) { printGroups($list->{groups}, %group); print $list->{line}; print "\n"; }
Print only uniq paragraphs-
sub uniq { my %seen; grep !$seen{$_}++, @_; } for my $list (@lists) { my @filtered = uniq(@lists); printGroups($filtered->{groups}, %group); print $list->{line}; print "\n"; }
But's it's not working and gives error- undefined value as an ARRAY.... Please let me know if you see anything lacking

Replies are listed 'Best First'.
Re: Print Uniq
by choroba (Cardinal) on Jul 11, 2016 at 15:09 UTC
    What I see lacking:
    • the definition of the printGroups subroutine
    • how @lists are populated
    • what's inside %group
    • strict as $filtered and @filtered are two different variables

    The problem might be that uniq is called on @lists , not just $list . It's not clear, though, whether you want to have unique paragraphs per list, or across all the lists. In the second case, you should populate the @filtered array outside of the loop.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Print Uniq
by haukex (Archbishop) on Jul 11, 2016 at 15:48 UTC

    Hi ArifS,

    You should probably provide some context for your question by linking to the thread this question comes from (you posted a near-duplicate of your question here). I took a look at the code and various sample inputs from that thread, and it is unclear to me which duplicates your are trying to detect - when I look at the @lists variable I hardly see any duplicates, except maybe in the "groups" item of the data structure*.

    The best way to get answers is to provide: 1. sample input (in this case you're working with Perl data structures, so use Data::Dumper to output them), 2. runnable code we can use to test, and 3. the expected output and the actual output including exact error messages. See also How do I post a question effectively? and "SSCCE".

    * When I use this code on this sample input data, here's what @lists looks like:

    ( { groups => ["INSIDE_IN", "WEB-CLIENT", "WEB-SERVER", "WEB_TCP"], line => "access-list INSIDE_IN extended permit tcp object-group +WEB-CLIENT object-group WEB-SERVER object-group WEB_TCP\n", }, { groups => ["INSIDE_IN", "EMAIL-CLIENT", "EMAIL-SERVER", "SMTP_TCP" +], line => "access-list INSIDE_IN extended permit tcp object-group +EMAIL-CLIENT object-group EMAIL-SERVER object-group SMTP_TCP\n", }, { groups => ["INSIDE_IN", "WEB_TCP"], line => "access-list INSIDE_IN extended permit tcp object-group +MYSRVR_2nd object-group MYCLIENTS_1st object-group WEB_TCP\n", }, )

    Hope this helps,
    -- Hauke D

Re: Print Uniq
by GotToBTru (Prior) on Jul 11, 2016 at 16:58 UTC

    You apply your uniq function when your for loop already is working on the array containing duplicates.

    sub uniq { my %seen; grep !$seen{$_}++, @_; } my @filtered = uniq(@lists); for my $list (@filtered) { printGroups($list->{groups}, %group); print $list->{line}; print "\n"; }

    This makes a little more sense, but your uniq function is not doing what you think. It would work if @lists contained scalars, but it doesn't. You need to modify the hash key to produce a scalar value. This will be determined by what constitutes a duplicate. Is it the lines are the same? Or is it based on how many values are in groups? Or what values are in groups? If the first, you want $_->{line} instead of $_.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Print Uniq
by dorko (Prior) on Jul 11, 2016 at 18:21 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-24 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found