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

I need help extracting part of a list. I have a list that is structured as such:
data1 data2 data3 data4 data5
I need to define data1 and data4 as bad data and extract and print data2, data3, and data5. How can I do that?

Please allow me to rephrase ...

I have an original list that looks like so: (it is much longer but this will give the idea)
"601-4001", "Disaster, Recovery" "601-4066", "Dial, " "601-4080", "Business, Line, " "601-4101", "Eric, Forward" "601-4102", "Jimmy, FWD" "601-4261", "Scott, " "601-4262", "Cecelia, " "601-4263", "Frederick, " "601-4269", "Rick, " "601-4270", "KENNETH, " "601-4272", "Sandra, " "601-4273", "800, redirect" "601-4274", "Suzanne, "
I want to identify the non-human entries as bad data and print the rest. I have this so far but do not know how to print what is not identified:
open(PBXFILE, "c:\/gabi\/global_address.txt") || die "cannot open: $! +"; while (<PBXFILE>) { chomp; {s/"//g } {s/,//g } } # Create a blacklist of numbers that do not map to a person @blacklistA = qw( 601-4001 Disaster Recovery 601-4066 Dial 601-4080 Business Line 601-4101 Eric Forward 601-4102 Jimmy FWD 601-4273 800 redirect );
can someone guide me?

Replies are listed 'Best First'.
Re: Extract part of a list
by Fletch (Bishop) on Jul 28, 2008 at 13:47 UTC

    Use the Montgomery Scott transform:

    • Pick up your mouse and hold it to your mouth.
    • Say, "Computer, data1 and data4 are bad data. Extract and print good data. Make it so."
    • Profit!

    Alternately, read the documentation for grep and see How (Not) To Ask A Question.

    (N.B. Didn't work for Scotty either, actually . . .)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      ...fletch... This is not working... mmm... where is that darn mute button!

      Pancho
Re: Extract part of a list
by moritz (Cardinal) on Jul 28, 2008 at 13:48 UTC
Re: Extract part of a list
by apl (Monsignor) on Jul 28, 2008 at 14:44 UTC
    Are you asking how to parse the five fields from a line of text? If so, how would you determine the end of one field, and the start of another? What if there's four fields? Six fields?

    Are you asking how to specify certain elements of a structure are "bad"? What type of a structure (hash, array, something else) are you using?

    How do you define "bad"?

Re: Extract part of a list
by baxy77bax (Deacon) on Jul 28, 2008 at 14:46 UTC
    one possible solution is (if i understood the question) :
    use strict; my @notgood = qw(1 5 6 9); my @goodarray = qw(1 2 3 4 5 6 7 8 9); my @good; foreach my $yes (@goodarray){ if (!grep{$_ == $yes}@notgood){ push @good , $yes; } } print "@good";
    but read how grep works and you'll probably find more elegant way to do this

      I personally believe that to expand a little bit on the subject, I may mention that that code may be cast in the form of two nested greps:

      my @good = grep { my $yes = $_; !grep $_ == $yes, @notgood; } @goodarray;

      One minor inconvenience, with the inner one, is that it scans all of @notgood also when it has already found one matching element: of course we're generally not manic about this because we don't care premature optimization; but I somewhat psychologically feel uneasy with it. Then List::Util's first() comes to the rescue:

      my @good = grep { my $yes = $_; !first {$_ == $yes} @notgood; } @goodarray;

      Too bad it's not a builtin in the first place. But then 5.10 can do even better, with its smart match operator:

      my @good = grep { not @notgood ~~ $_ } @goodarray;

      And that's it!

      --
      If you can't understand the incipit, then please check the IPB Campaign.