there are two arrays in arrayA and arrayB, I wish to match the list between them regardless of the number(1 2 3 or...) to list down which is not in arrayB compare to arrayA - @arrayA = qw (A,3 B,4 D,5 E,6 );
-
@arrayB = qw (A,3 B,5 C,5);
-
-
my @NotInB = do {
-
my %inA = map { $_ ,1} (@arrayB,(split',')[0]);
-
grep (!$inA{$_}, (@arrayA,(split',')[0]));
-
};
-
-
print Dumper @NotInB;
suppose i wish to get
$VAR1 = 'D,5';
$VAR2 = 'E,6';
but i get the result as...
$VAR1 = 'B,4';
$VAR2 = 'D,5';
$VAR3 = 'E,6';
please guide... thanks
9 1420 This should get you started on the path to comparing your arrays. Let us know if you get stuck.
Regards,
Jeff
I am not very clear on your question. Is that you need to consider only the alphabets in the alphabet-number pairs while comparing those two arrays?
In that case, the following code will do the job: -
use Data::Dumper;
-
-
@arrayA = qw (A,3 B,4 D,5 E,6 );
-
@arrayB = qw (A,3 B,5 C,5);
-
-
my %inA = map { $_ ,1} map { ((split /,/,$_)[0])} (@arrayB);
-
-
foreach(@arrayA) {
-
my $temp= (split /,/,$_)[0] ;
-
push @NotInB,$_ if(!(exists $inA{$temp}));
-
}
-
-
print Dumper @NotInB;
-
Yup, that's what i want. Thank you both of you. I appreaciate it.
I have last question, how if i wish to deduct the number when i compare arrayA and arrayB if it's exist in arrayB.
result as below:
$VAR1 = 'A,0';
$VAR2 = 'B,1';
$VAR3 = 'C,5';
Please give me some hints on how to do the it. Thanks.
Yup, that's what i want. Thank you both of you. I appreaciate it.
I have last question, how if i wish to deduct the number when i compare arrayA and arrayB if it's exist in arrayB.
result as below:
$VAR1 = 'A,0';
$VAR2 = 'B,1';
$VAR3 = 'C,5';
Please give me some hints on how to do the it. Thanks.
For this, you can split both arrays further on commas and assign the resulting arrays to hashes(alphabets as keys and succeeding numbers as respective values).
Further you can parse through @arrayB and take out the alphabets, as in previous code. Try to check if the key exists in %hashA, if so substract their respective values and push the key and resulting value to result array.
Here it is: -
use Data::Dumper;
-
@arrayA = qw (A,3 B,4 D,5 E,6 );
-
@arrayB = qw (A,3 B,5 C,5);
-
#splitting elements initial array across ',' and creating a new array
-
push @splitA,split(/,/,$_) foreach (@arrayA);
-
-
%hashA = @splitA;
-
foreach(@arrayB) {
-
# split the element into key-value pair
-
my ($key,$val)= (split /,/,$_);
-
-
if(!(exists $hashA{$key})) {
-
push @result,$_ ;
-
} else {
-
my $diff=abs($val-$hashA{$key}) ; # absolute diff. between respective numbers
-
my $str = join(',',($key,$diff));
-
push @result,$str ;
-
}
-
}
-
print Dumper @result;
-
-
Hi nithinpes,
Thanks.
I try to play around with your codes.. where I added more number (A,3 --> A,3,4)
as below:
@arrayA = qw(A,3,4, B,8,7, C,9,3);
@arrayB = qw(A,5,2 B,5,3 T,9,2);
By that, your above code no longer can be reused. I think i should create the hashes of arrays right? e.g %hash = ('A' => ['3', '4'], 'B' => ['8','7'], 'C' => ['9','3']);
Is it the way to do it? If so, how to created the hashes of arrays?
Thanks.
Hi nithinpes,
Thanks.
I try to play around with your codes.. where I added more number (A,3 --> A,3,4)
as below:
@arrayA = qw(A,3,4, B,8,7, C,9,3);
@arrayB = qw(A,5,2 B,5,3 T,9,2);
By that, your above code no longer can be reused. I think i should create the hashes of arrays right? e.g %hash = ('A' => ['3', '4'], 'B' => ['8','7'], 'C' => ['9','3']);
Is it the way to do it? If so, how to created the hashes of arrays?
Thanks.
Well, the code that I posted was particular to the type of sample array that you posted(an alphabet followed by a number). If you want to extend this to alphabet followed by any number of numeric characters, creating hash of arrays is the good option.
You can parse each element of the array and do pattern match. If you get an alphabetic character, make it the key and push the following numeric characters(until you match an alphabet) to it's value(array).
- Nithin
I feel the following would be a better approach. The third argument of split() function will define the number of elements to be split into. In this case it is 2, the first element would be the alphabet and the next element will be comma-separated numbers. -
@arrayA = qw(A,3,4, B,8,7, C,9,3);
-
push @splitA,split(/,/,$_,2) foreach(@arrayA );
-
## convert it to a hash (one-dimensional)
-
%hashA=@splitA;
-
-
## create hash of arrays
-
foreach(keys %hashA) {
-
# split the comma-separated numbers in value into an anonymous array
-
$newhashA{$_} = [split /,/,$hashA{$_}];
-
}
-
-
print Dumper %newhashA;
-
Hi nithinpes,
THANKS you so much for your continuous guidance and help.
I learnt a lot from you.
Thank you & God Bless.
:)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: fartsniff |
last post by:
i found this code out in the ng, and its seems long and clunky,
i am still experimenting with preg_match and _replace, but the
syntax is a bit confusing. it seems i always misplace or mistype...
|
by: Ravi |
last post by:
Hi,
I have about 200GB of data that I need to go through and extract the
common first part of a line. Something like this.
>>>a = "abcdefghijklmnopqrstuvwxyz"
>>>b = "abcdefghijklmnopBHLHT"...
|
by: Canonical Latin |
last post by:
"Leor Zolman" <leor@bdsoft.com> wrote
> "Canonical Latin" <javaplus@hotmail.com> wrote:
>
> > ...
> >But I'm still curious as to the rational of having type
>...
|
by: Timmy |
last post by:
I'm working on a simple click-through image gallery and I have images with
captions in two arrays like this:
var current_value="0";
var images = new Array
("photo01.jpg",
"photo02.jpg",...
|
by: Rob Griffiths |
last post by:
Can anyone explain to me the difference between an element type and a
component type?
In the java literature, arrays are said to have component types, whereas
collections from the Collections...
|
by: stealth_spoof |
last post by:
Hi People wondering if anyone can help me with a problem I'm having
I'm trying to create an array with an unspecified length, the length is
based on the result i get from another task in the code...
|
by: Rene Nyffenegger |
last post by:
Hello everyone.
I am not fluent in JavaScript, so I might overlook the obvious.
But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in...
|
by: fariba123 |
last post by:
there are two arrays:
$players = array(
"23" => "Michael Jordan",
"32" => "Michael Johnson"
);
$current_player = array(
"player_id" => "23",
"age" => "22"
|
by: somebody |
last post by:
There are two files below named search.c and search.h.
In the for loop in search.c, the for loop never exits,
even if mystruct.field1 has no match. Instead of
exiting the for loop it keeps going...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |