Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: My 'perldar' tells me there is a 'better' solution for this list operation

by johngg (Canon)
on Nov 04, 2006 at 10:35 UTC ( [id://582234]=note: print w/replies, xml ) Need Help??


in reply to My 'perldar' tells me there is a 'better' solution for this list operation

Or how about a solution using a regular expression.

my @l1 = qw(Date IndexID Maturity OnTheRun CompositePrice CompositeSpread ModelPrice ModelSpread Depth Heat); my @l2 = qw(OnTheRun CompositePrice CompositeSpread Depth); my @l3; { local $" = q{|}; @l3 = map {m{^@l2$} ? $_ : q{null}} @l1; }

Cheers,

JohnGG

  • Comment on Re: My 'perldar' tells me there is a 'better' solution for this list operation
  • Download Code

Replies are listed 'Best First'.
Re^2: My 'perldar' tells me there is a 'better' solution for this list operation
by an0 (Initiate) on Nov 04, 2006 at 16:21 UTC

    To johngg:
    it seems your code does not work at all; even if rectified to work, it is not so efficient when regex engine is involved.

    My suggestion is:
    sort (lexicographically) the lists first, then the search can be done in one whole pass through the two lists.

      It works fine for me. In what way did it not work for you? I did not show the use strict; and use warnings; I always put at the top of my scripts in my first post. Here's the whole script again, it's output and some info on my platform and perl version

      $ cat spw582186 #!/usr/bin/perl # use strict; use warnings; my @l1 = qw(Date IndexID Maturity OnTheRun CompositePrice CompositeSpread ModelPrice ModelSpread Depth Heat); my @l2 = qw(OnTheRun CompositePrice CompositeSpread Depth); my @l3; { local $" = q{|}; @l3 = map {m{^@l2$} ? $_ : q{null}} @l1; } print qq{$_\n} for @l3; $ ./spw582186 null null null OnTheRun CompositePrice CompositeSpread null null Depth null $ uname -a SunOS b4rsk 5.10 Generic_118833-03 sun4u sparc SUNW,Ultra-60 $ perl -v This is perl, v5.8.4 built for sun4-solaris-64int (with 28 registered patches, see perl -V for more detail) Copyright 1987-2004, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.com/, the Perl Home Pa +ge. $

      You may well be right regarding efficiency, I haven't run any benchmarks. The object of the post was to show an alternative method to those already given.

      Cheers,

      JohnGG

        Hi, John.

        Looks like you posted this as I was composing my reply below. I get it now. You're doing it exactly how I was thinking of the problem at first, making the regular expression be the array and the LHV of the match be the individual element. That's a clever use of $" to make the separator be the alternation operator for regexps ('|'). I thought | only worked in parentheses, though. I stand corrected.

        _________________________________________________________________________________

        I like computer programming because it's like Legos for the mind.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found