MAP question

N

Noname

I know I am wrong some where but could not figure it out , though it
may sound silly to some ,
I have an array from which I am trying to create hash, below is the
code line,
my %ALLRELEASES=map {chomp $_} @_tem;
all I get is empty has, if I don't use "chomp" i get atleast some
elements
 
P

Paul Lalli

Noname said:
I know I am wrong some where but could not figure it out , though it
may sound silly to some ,
I have an array from which I am trying to create hash, below is the
code line,
my %ALLRELEASES=map {chomp $_} @_tem;
all I get is empty has, if I don't use "chomp" i get atleast some
elements

You are confused about the return value of the 'chomp' function. It does
not return the chomped string. Please see:
perldoc -f chomp

If you wanted to use this method, you would have to return $_ from map's
block:
my %allReleases = map { chomp $_; $_ } @_tem;

However, the question needs to be asked as to what you think you're
doing with this? Does your array contain a list of key/value pairs,
such as:
@_temp = ('one', 1, 'two', 2, 'three', 3);

If so, you don't need map at all. Just assign a hash to the values from
the array:

%allReleases = @_tem;

If you want to apply the 'chomp' function to all elements of the array,
do so:
chomp @_tem;
(Just don't try to combine those two statements into one - for the same
reason as your initial error)

Paul Lalli
 
A

Arndt Jonasson

Noname said:
I know I am wrong some where but could not figure it out , though it
may sound silly to some ,
I have an array from which I am trying to create hash, below is the
code line,
my %ALLRELEASES=map {chomp $_} @_tem;
all I get is empty has, if I don't use "chomp" i get atleast some
elements

(Slight newbie-warning - I'm not experienced myself.)

You could give an example of the contents of @_tem. It's pretty much
guesswork now. At least one thing that is likely to be wrong is that
'chomp' doesn't return the chomped string - it returns the number of
characters removed, as "perldoc -f chomp" will tell you.

This might be what you want, but maybe not:

my %ALLRELEASES=map {chomp $_; $_} @_tem;

I don't know how @_tmp was constructed, but I would probably want
to do the chomping already before the strings went into the array.
Then you would only need

my %ALLRELEASES= @_tem;

You could chomp all the strings in the list, the doc says, so we can do
it this way:

chomp @_tem;
my %ALLRELEASES= @_tem;
 
N

Noname

Paul said:
You are confused about the return value of the 'chomp' function. It
does not return the chomped string. Please see:
perldoc -f chomp

If you wanted to use this method, you would have to return $_ from
map's block:
my %allReleases = map { chomp $_; $_ } @_tem;

However, the question needs to be asked as to what you think you're
doing with this? Does your array contain a list of key/value pairs,
such as:
@_temp = ('one', 1, 'two', 2, 'three', 3);

If so, you don't need map at all. Just assign a hash to the values
from the array:

%allReleases = @_tem;

If you want to apply the 'chomp' function to all elements of the
array, do so:
chomp @_tem;
(Just don't try to combine those two statements into one - for the
same reason as your initial error)

Paul Lalli

I realized my mistake, of chomp, thanks
but now the question in @_tem array has values
('one','two','three','four);
which when initializes as > %allReleases = @_tem;
it becomes one=>two,three=>four,
what I want is all the values in array becomes keys of hash
I hope I am talking some sence
 
P

Paul Lalli

Noname said:
but now the question in @_tem array has values
('one','two','three','four);
which when initializes as > %allReleases = @_tem;
it becomes one=>two,three=>four,
what I want is all the values in array becomes keys of hash

Well you should have specified that originally! ;-) Indeed, you still
haven't given a complete problem description, because you haven't told
us what you want the values of the hash to be. I will make a random
assumption that you want each value of the hash to be the number 1.

In that case, you can use a hash slice:
@allReleases{@_tem} = (1) x @_tem;

This creates a list of 1s on the right (of the same size as @_tem), and
assigns each position of %allReleases corresponding to the values of
@_tem to a value of 1.

If that syntax is confusing, you could do it with a loop instead:

$allReleases{$_} = 1 foreach @_tem;
I hope I am talking some sence

Some, but not enough. ;-) If you're looking for the values of the hash
to be something other than a constant integer, please post a complete
problem description.

Paul Lalli
 
A

Arndt Jonasson

Noname said:
I realized my mistake, of chomp, thanks
but now the question in @_tem array has values
('one','two','three','four);
which when initializes as > %allReleases = @_tem;
it becomes one=>two,three=>four,
what I want is all the values in array becomes keys of hash
I hope I am talking some sence

You seem to be talking about a hash table with no values in it. This does
make sense, if you want the hash table to just be a collection of strings
that allows for fast lookup whether a given string is present or not.

The line below constructs a hash table with keys as given in @_tem,
with the "undefined value" as value for them. It does this by specifiying
a hash slice (of a hash table that doesn't exist yet, but that's Perl).

@allReleases{@_tem} = undef;

If you do want to associate the keys with values in your hash table,
you need to let the values enter the computation somewhere.
 
T

Thomas Kratz

Paul said:
Well you should have specified that originally! ;-) Indeed, you still
haven't given a complete problem description, because you haven't told
us what you want the values of the hash to be. I will make a random
assumption that you want each value of the hash to be the number 1.

In that case, you can use a hash slice:
@allReleases{@_tem} = (1) x @_tem;

This creates a list of 1s on the right (of the same size as @_tem), and
assigns each position of %allReleases corresponding to the values of
@_tem to a value of 1.

If that syntax is confusing, you could do it with a loop instead:

$allReleases{$_} = 1 foreach @_tem;

Or:

my %allReleases = map { $_ => 1 } @_tem;

Then you don't have to declare %allReleases beforehand under strict.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
A

A. Sinan Unur

You seem to be talking about a hash table with no values in it. This
does make sense, if you want the hash table to just be a collection of
strings that allows for fast lookup whether a given string is present
or not.

The line below constructs a hash table with keys as given in @_tem,
with the "undefined value" as value for them. It does this by
specifiying a hash slice (of a hash table that doesn't exist yet, but
that's Perl).

@allReleases{@_tem} = undef;

If you do want to associate the keys with values in your hash table,
you need to let the values enter the computation somewhere.

The above is misleading in that it creates the impression that one could
replace the undef above with anything else and still use it this way.
That is patently untrue:

use strict;
use warnings;

my @keys = qw(one two three four five);
my %hash;
@hash{@keys} = 1;

use Data::Dumper;
print Dumper \%hash;

__END__

Run it and see what it produces.

Thus, if you want to use a hash-slice rather than the foreach loop
solution suggested by Paul Lalli, you should do:

use strict;
use warnings;

my @keys = qw(one two three four five);
my %hash;
@hash{@keys} = (1) x @keys;

use Data::Dumper;
print Dumper \%hash;

__END__

Sinan
 
A

Anno Siegel

A. Sinan Unur said:
The above is misleading in that it creates the impression that one could
replace the undef above with anything else and still use it this way.

Yup, misleading also to a casual reader of the program. It explicitly
assigns undef() to one hash element and leaves the others alone for no
good reason. While the effect is the same, the correct way of writing
this is to leave all elements unspecified:

@allReleases{@_tem} = ();

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
While the effect is the same, the correct way of writing
this is to leave all elements unspecified:

@allReleases{@_tem} = ();

When I re-read my post, I realized that I did not make it clear that

@allReleases{@_tem} = undef;

was setting only one element to undef. Thanks for the clarification.

Sinan
 
A

Arndt Jonasson

Yup, misleading also to a casual reader of the program. It explicitly
assigns undef() to one hash element and leaves the others alone for no
good reason. While the effect is the same, the correct way of writing
this is to leave all elements unspecified:

@allReleases{@_tem} = ();

This is much better, of course. Thanks for correcting me. (Going by mere
impressions is of course not a good thing to do.)
 

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
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top