Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > 2 array problem

Reply
Thread Tools

2 array problem

 
 
katera
Guest
Posts: n/a
 
      05-04-2007
Help

I have 2 array @a @b
How to exclude second array from first?

Eg
@a= ("123;first", "124;second", "013;bla3", "011;bla4", "050,bla5");
@b= (123,011,000,050);


result:
@c= ("124;first", "013;bla3");

Thanks


 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      05-04-2007
On Fri, 4 May 2007 09:30:39 +0200, "katera" <(E-Mail Removed)> wrote:

>I have 2 array @a @b
>How to exclude second array from first?


perldoc -q difference

>Eg
>@a= ("123;first", "124;second", "013;bla3", "011;bla4", "050,bla5");
>@b= (123,011,000,050);
>
>
>result:
>@c= ("124;first", "013;bla3");


Not exactly the same as the FAQ entry. Try to adapt that as an
exercise. If you can't, come back and ask for more help.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      05-04-2007
katera <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Help
>
> I have 2 array @a @b


You should avoid the variable names "a" and "b" in Perl, they have
special significance and don't behave like other variables in
all respects.

> How to exclude second array from first?
>
> Eg
> @a= ("123;first", "124;second", "013;bla3", "011;bla4", "050,bla5");

^
Why is the separator in the last element a comma? I'll assume it's
a semicolon like the others.

> @b= (123,011,000,050);


You are specifying the elements of @b as numbers with leading zeroes,
but these are lost in evaluation. The number 050 will not match the
string "050" in "050;bla5". Specify @b as strings.

> result:
> @c= ("124;first", "013;bla3");


There is no "124;first" in array @a but only "124;second".

In summary, every one of your three lines of problem description
contains an error. Please take more care, the inconsistencies
make it time-consuming just to find out what you want to achieve.

Here is one way:

my @a = ("123;first", "124;second", "013;bla3", "011;bla4", "050;bla5");
my @b = qw(123 011 000 050);

my @c = do {
my %h = map split( /;/) => @a;
delete @h{ @b};
map "$_;$h{ $_}" => keys %h;
};

print "@c\n";

Anno
 
Reply With Quote
 
phus
Guest
Posts: n/a
 
      05-04-2007

katera д
> Help
>
> I have 2 array @a @b
> How to exclude second array from first?
>
> Eg
> @a= ("123;first", "124;second", "013;bla3", "011;bla4", "050,bla5");
> @b= (123,011,000,050);
>
>
> result:
> @c= ("124;first", "013;bla3");
>
> Thanks

try

my @a= ("123;first", "124;second", "013;bla3", "011;bla4",
"050;bla5");
my @b= ('123','011','000','050'); # not 123 but '123', etc
my @c = grep {!exists(${{map {$_=>1} @b}}{(m/^(\d+)/)[0]})} @a;
print "@c\n";

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-04-2007
On 4 May 2007 10:06:25 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:

>> @b= (123,011,000,050);

>
>You are specifying the elements of @b as numbers with leading zeroes,
>but these are lost in evaluation. The number 050 will not match the
>string "050" in "050;bla5". Specify @b as strings.


And, (to the OP) a cheap way to do so is to put a qw in front of the
parenthesis and make those commas into spaces.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      05-04-2007
Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed)-berlin.de ((E-Mail Removed)-berlin.de) wrote
> on MMMMCMXCIV September MCMXCIII in <URL:news:(E-Mail Removed)>:
> @@ katera <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> @@ > Help
> @@ >
> @@ > I have 2 array @a @b
> @@
> @@ You should avoid the variable names "a" and "b" in Perl, they have
> @@ special significance and don't behave like other variables in
> @@ all respects.
>
> Well, that's only $a and $b.


Yes, but they're package variables, so @a and $a aren't completely
independent.

use strict; use warnings;
my ( @l);

# a legit use of $a and $b
@l = sort { $a <=> $b } 1, 2, 3;

# ... makes @a behave differently
@l = @main::a; # no warning "once" here

# ... from @x
@l = @main:; # this warns

Here the "...used only once" warning is not issued for @a. If the
"sort" line is commented out, the warning appears for both @a and @x.

Anno
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      05-04-2007
On 04 May 2007 15:07:18 GMT, Abigail <(E-Mail Removed)> wrote:

>That '@a' doesn't warn in your example has little to do with @a being
>special. It just has to do with the fact that *a exists. Which exists


That's surely what Anno meant. As you surely know full well.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-04-2007
>>>>> "p" == phus <(E-Mail Removed)> writes:

p> my @a= ("123;first", "124;second", "013;bla3", "011;bla4",
p> "050;bla5");
p> my @b= ('123','011','000','050'); # not 123 but '123', etc
p> my @c = grep {!exists(${{map {$_=>1} @b}}{(m/^(\d+)/)[0]})} @a;

yow!! do you realize that you are building the anon exists hash for EACH
iteration of the grep? that hash should be built before the grep line
and used inside to replace the map.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      05-06-2007
Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Michele Dondi ((E-Mail Removed)) wrote on MMMMCMXCIV September
> MCMXCIII in <URL:news:(E-Mail Removed) om>:
> <> On 04 May 2007 15:07:18 GMT, Abigail <(E-Mail Removed)> wrote:
> <>
> <> >That '@a' doesn't warn in your example has little to do with @a being
> <> >special. It just has to do with the fact that *a exists. Which exists
> <>
> <> That's surely what Anno meant. As you surely know full well.
>
>
> Then I don't get it. @a behaves the same as @c.


True, but the specialness of $a (the package variable) makes it easier
to use it inadvertently. That way @c is slightly safer than @a.

Anno
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      05-06-2007
Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed)-berlin.de ((E-Mail Removed)-berlin.de) wrote
> on MMMMCMXCVI September MCMXCIII in <URL:news:(E-Mail Removed)>:
> Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > Michele Dondi ((E-Mail Removed)) wrote on MMMMCMXCIV September
> > MCMXCIII in <URL:news:(E-Mail Removed) om>:
> > <> On 04 May 2007 15:07:18 GMT, Abigail <(E-Mail Removed)> wrote:
> > <>
> > <> >That '@a' doesn't warn in your example has little to do with @a being
> > <> >special. It just has to do with the fact that *a exists. Which exists
> > <>
> > <> That's surely what Anno meant. As you surely know full well.
> >
> >
> > Then I don't get it. @a behaves the same as @c.
>
> True, but the specialness of $a (the package variable) makes it easier
> to use it inadvertently. That way @c is slightly safer than @a.
>
>
> I still fail to get it. You seem to be saying "because $a is special,
> you might not get the warning @main::a used only once". But that is
> only a useful warning if you *mistype* a variable name - in which case
> it's safer to use @a (which if mistyped to @c triggers the warning -
> or triggers a strict error) than to use @c (which if mistypes to @a may
> not trigger the warning or error). But that doesn't make much sense.


The advice would have to be never to misspell "a" or "b" when you intend
to write something else. I guess you got me there.

So the use of @a, @b, %a, and %b as package variables (or lexicals)
seems to be technically no problem. The lexicals $a and $b should be
avoided if there is a chance you want to run sort() in the same
scope. There always is; you'd have to use a prototyped comparison
function, a major nuisance.

If one is interested in them at all, the package variables $::a and
$::b are less problematic than they would seem to be. If used in a
sub, you must localize them, but that goes for all package variables
that aren't meant to be global. $a and $b are only special in that
they can't reasonably be used globally.

The reason to avoid them would be that from "perldoc -f sort" one
would expect sort() to overwrite them randomly. But that doesn't
happen:

( $a, $b) = ( 99, 100);
my @s = sort { $b <=> $a } 1 .. 4;
print "$a $b\n";

still prints "99, 100". The preserving behavior has been around for
a long time, but it isn't documented or I overlooked it more than once.
Apart from that, there doesn't seem to be much reason to avoid $::a
and $::b at all, except, of course, who wants them in the first place.

Lexical $a and $b would be nice, but they remain problematic.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Array Problem, sort Array Cool Wong Ruby 17 06-30-2007 03:38 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Problem assigning an Array object to an Array-subclass object Richard Lionheart Ruby 27 05-04-2004 06:42 AM



Advertisments