CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Comparing strings/arrays.

 



newbiecoder
Novice

Mar 17, 2003, 8:44 AM

Post #1 of 3 (230 views)
Comparing strings/arrays. Can't Post

Hi,
I have two strings:
$input1 = "1|2|3|4|5";
$input2 = "2|3|5";

What I'm trying to do is compare the two and create a third string containing the values that aren't in $input2 but are in $input1. e.g. 1|4 as they are not in $input2

I am trying to do this in the following way, but obviously it's not working, this lists the ones that are their:



Code
@input1 = split(/\|/, $input1); 
@input2 = split(/\|/, $input2);
foreach my $input2a (@input2) {
$output = "0";
foreach my $input1a (@input1) {
if ($input2a eq $input1a) {
$output = "1";
}
}
if ($output eq "1") {
$output_values = "$input2|$output_values";
}
}


All help is very much appreciated!
Thanks,
Chris


Paul
Enthusiast

Mar 17, 2003, 11:03 AM

Post #2 of 3 (226 views)
Re: [newbiecoder] Comparing strings/arrays. [In reply to] Can't Post


Code
my $input1 = "1|2|3|4|5"; 
my $input2 = "2|3|5";
my @split = split /\|/, $input2;
my @spare = ();

for my $this (split /\|/, $input1) {
unless (grep $this == $_, @split) {
push @spare, $this;
}
}

my $final = join "|", @spare;



(This post was edited by Paul on Mar 17, 2003, 11:03 AM)


newbiecoder
Novice

Mar 18, 2003, 3:48 AM

Post #3 of 3 (219 views)
Re: [Paul] Comparing strings/arrays. [In reply to] Can't Post

Thank you! It worked perfectly.
Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives