Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding a string in an array of strings 1

Status
Not open for further replies.

soy4soy

IS-IT--Management
May 22, 2000
100
US
Ok, I know I should know this by now, but I always get in a "chicken or the egg" logic loop here.

I am looking to write/steal a function that will take an array as input, then:

1) remove blank lines, and,
2) remove redundant (same) lines

So, if I had an array:

@arry1 = (
"This is line one",
"This is line two",
"This is line three",
" \n",
"\t\n",
"This is line one",
"This is line two",
"This is line three" );

and I then ran it through a function, say like this:

print condense(@arry1);

it would produce this result:

This is line one
This is line two
This is line three

my code looks like this, thus far:

sub condense {
my @done = ();
foreach (@_) {
if(tr/\S+//) { #if there is any number of non-whitespace characters...
push( @done, $_ ) if (@done =~ grep{/$_/} @done);
}
}
return @done;
}



My problem is I am trying to find a string $unvalidated in @good_strings.

Is this
 
Ooops! yeah... well that's my code.

I am getting an error on the "if(tr/\S+//){" line:

"Unrecognized escape \S..."

I thought that weould work, but I will also need help in finding that string in the array... I think I was on the right track at one point. ;-)

Thanks!
 
you can use the grep() function to filter arrays quite nicely

give grep() an array and a regex pattern and it will return an array containing all of the elements that match that pattern

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
this seems to work.. (tr/// doesn't handle regular expressions)
Something seems to happen to $_ somewhere in there so I assigned it to $string instead of using the default.

Code:
sub condense {
    my @done = ();
    foreach $string (@_) {
        if($string =~ /\S+/) {
            unless (grep(/$string/,@done)){
                push (@done,$string);
            }
        }
    }

    return @done;
}
 
I always liked using hashes whenever possible for stuff like that. It's almost always faster, but generally not as memory friendly. Dunno, take your pick.
Code:
sub condense
{
	my %hash;
	grep { /\S/ && !exists $hash{$_} && ($hash{$_} = 1) } @_;
	return keys %hash;
}

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Thanks chaz!

I really needed the syntax on that grep function!
 
ok.....

explain the condense() function for the slower amongst us (me)

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
:) I assumed it takes a list of strings and returns a list of the unique elements that contain more than just whitespace. Close enough, soy?

grep syntax: perldoc.com, for all your standard perl needs.
cpan.org, for everything else.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top