Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Need to Create an Array of Differences

by xtype (Deacon)
on Aug 03, 2004 at 18:30 UTC ( [id://379777]=note: print w/replies, xml ) Need Help??


in reply to Need to Create an Array of Differences

Here is another take on the lookup hash, which I use now and then.
Much the same as Aristotle's bit. Although, I thought that I would
throw it in anyway, since I had it ready before reading the replies.
## use hash search to find everyone in @a but not in @b. my %temphash = map { $_ => 1 } @b; my @c = grep { !$temphash{ $_ } } @a;

-xtype

Replies are listed 'Best First'.
Re^2: Need to Create an Array of Differences
by Anonymous Monk on Dec 16, 2004 at 15:46 UTC
    ther's a better way to use grep...
    
    my @difference =  grep /^@a1/, @a2;
    
    #example of sub...
    
    my $difference = diff(\@a1,\@a2);
    
    sub diff {
        my @diff =  grep /[^@{$_[0]}]/, @{$_1};
        return \@diff;
    }
    
    Marco (nemux)
    Bye.
    
      sorry ... is impossible to see in my previous message the -> [] 
      
      then with square brackets visible is :
      
      sub diff_2 {
          my @diff =  grep /[[^@{$_[0]]}]/, @{$_[1]};
          return \@diff;
      }
      
      Marco
      
      
        Better?
        If that worked, it wouldn't be semantically the same as Aristotle's, Zaxo's, or my reply. Nor of the perlfaq or tachyon's (which show a difference of both arrays).

        Did you test your code? It is a nice thought, however, I cannot make that work on 5.6.1, 5.8.1, or 5.8.5
        For a simple array of int's @diff is always undef.
        For an array containing scalars I get "Invalid range in regex" error, which is what I expect.

        -xtype

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found