[newbie] list elements

G

Guest1

If I have an array @a = [ 1 2 3 4 2 3 ];

how I can get just the elements that are not the same?

1, 2, 3 and 4

and what is the best way to store these values?

Thanks
Pedro
 
G

Gunter Schelfhout

Guest1 said:
If I have an array @a = [ 1 2 3 4 2 3 ];

Try your array once in real code. ;-)
how I can get just the elements that are not the same?

1, 2, 3 and 4

A solution:
$seen{$_}++ foreach @a;
@unique = sort keys %seen;
and what is the best way to store these values?

Another array?

Buy the Perl cookbook. It has a lot of codesnippets to solve problems like
this.
 
G

Gunnar Hjalmarsson

Guest1 said:
If I have an array @a = [ 1 2 3 4 2 3 ];

Then you get a compilation error.
how I can get just the elements that are not the same?

1, 2, 3 and 4

Assuming that your array is:

@a = qw/1 2 3 4 2 3/;

you'd better study the FAQ:

perldoc -q duplicate
and what is the best way to store these values?

"Best way"? Don't understand the question.
 
G

Guest1

Thanks guys,

I am learning a lot with your help

I'll buy the cook book too.

P

Guest1 said:
If I have an array @a = [ 1 2 3 4 2 3 ];

Then you get a compilation error.
how I can get just the elements that are not the same?

1, 2, 3 and 4

Assuming that your array is:

@a = qw/1 2 3 4 2 3/;

you'd better study the FAQ:

perldoc -q duplicate
and what is the best way to store these values?

"Best way"? Don't understand the question.
 
V

Vlad Tepes

Guest1 said:
If I have an array @a = [ 1 2 3 4 2 3 ];
how I can get just the elements that are not the same?

I'll do as Gunnar, and assume you mean

@a = qw( 1 2 3 4 2 3 );
and what is the best way to store these values?

You could perhaps use the fact that hashes have unique keys.
Try building a hash with the values from the array:

map $hash{$_} = 1, @a; # this will be more efficient in later perls..

Then you can print the unique keys by making a call to keys(),

@unique = keys %hash;

If you want it in sequence, call sort (this is numerical sort):

@unique = sort { $a <=> $b } keys %hash;

HTH,
 
B

Brian McCauley

Guest1 said:
Thanks guys,

I am learning a lot with your help

I recommend that you learn to post correctly before your persistant
top-posting (which is considered rudeness) leads people to stop stop
helping you.

Please read posting guidelines to learn how to be a more constuctive
member of the comp.lang.perl.* community.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
J

Jürgen Exner

Guest1 said:
If I have an array @a = [ 1 2 3 4 2 3 ];

how I can get just the elements that are not the same?

Please see
perldoc -q duplicate
and what is the best way to store these values?

???
Please be more specific. What do you mean with "store"?

jue
 
J

John Bokma

Vlad said:
If I have an array @a = [ 1 2 3 4 2 3 ];
how I can get just the elements that are not the same?


I'll do as Gunnar, and assume you mean

@a = qw( 1 2 3 4 2 3 );
and what is the best way to store these values?


You could perhaps use the fact that hashes have unique keys.
Try building a hash with the values from the array:

map $hash{$_} = 1, @a; # this will be more efficient in later perls..

how about

my %hash;
@hash{@a} = ( "" ) x @a;
 
V

Vlad Tepes

John Bokma said:
Vlad said:
You could perhaps use the fact that hashes have unique keys.
Try building a hash with the values from the array:

map $hash{$_} = 1, @a; # this will be more efficient [...]

how about

my %hash;
@hash{@a} = ( "" ) x @a;

Why store anything at all? :)

@hash{@a} = ( undef ) x @a;

BTW, how can I find out how much memory a certain data-structure takes?
 
J

John Bokma

Vlad said:
John Bokma said:
Vlad said:
You could perhaps use the fact that hashes have unique keys.
Try building a hash with the values from the array:

map $hash{$_} = 1, @a; # this will be more efficient [...]

how about

my %hash;
@hash{@a} = ( "" ) x @a;


Why store anything at all? :)

@hash{@a} = ( undef ) x @a;

And what is undef? Is it not stored?
 
V

Vlad Tepes

John Bokma
Vlad Tepes
John Bokma
Vlad Tepes

You could perhaps use the fact that hashes have unique keys.
Try building a hash with the values from the array:

map $hash{$_} = 1, @a; # this will be more efficient [...]

how about

my %hash;
@hash{@a} = ( "" ) x @a;


Why store anything at all? :)

@hash{@a} = ( undef ) x @a;

And what is undef? Is it not stored?

I always thought that an empty string was something, while undef was
nada. I may be wrong. But... with a testfile

my @a = 0..1e5;
my %hash;
## @hash{@a} = ( "" ) x @a;
@hash{@a} = ( undef ) x @a;
$hash{234} = <STDIN>;

I got the following sizes when looking at output from ips(1):

empty strings: 26212 kB
undef values: 20700 kB

Some quick calculations give:

26212 - 20700 = 5512
5512 * 1024 / 1e5 = 56.44288

So setting a hash value to undef at least appears to save about
56 bytes memory compared to storing it as an empty string.

Wether undef actually is anything, I don't know.
If it isn't, then it can't be stored either ... or what?
 
J

John Bokma

Vlad Tepes wrote:

@hash{@a} = ( "" ) x @a;

v.s.

@hash{@a} = ( undef ) x @a;
So setting a hash value to undef at least appears to save about
56 bytes memory compared to storing it as an empty string.

56 bytes/element. That is quite some saving. Thanks for sharing.
I guess that undef just creates a null pointer and an empty string
creates a list element with an empty string and some info. (Wild guess)
 
J

John W. Krahn

John said:
Vlad Tepes wrote:

@hash{@a} = ( "" ) x @a;

v.s.

@hash{@a} = ( undef ) x @a;


56 bytes/element. That is quite some saving. Thanks for sharing.
I guess that undef just creates a null pointer and an empty string
creates a list element with an empty string and some info. (Wild guess)

You could just assign the empty list which would use less memory then
either of the above.

@hash{ @a } = ();


John
 
V

Vlad Tepes

John W. Krahn:
John Bokma:

You could just assign the empty list which would use less memory then
either of the above.

@hash{ @a } = ();

Indeed!

When I try to measure this once more, I get these results:

@a = 1..1e5;

@hash{@a} = (); => 19752 kB
@hash{@a} = ( undef ) x @a; => 20840 kB
@hash{@a} = ( "" ) x @a; => 26352 kB

Which means that relative sizes of the elements become:

@hash{@a} = (); => 0
@hash{@a} = ( undef ) x @a; => +11 bytes pr elem
@hash{@a} = ( "" ) x @a; => +68 bytes pr elem

( Difference between "" and undef is 57 this time, corresponding
well with my last attempt to measure the diff. )

Great John,
your way means less typing too, as usual...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,795
Members
47,342
Latest member
eixataze

Latest Threads

Top