Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > MAP question

Reply
Thread Tools

MAP question

 
 
Noname
Guest
Posts: n/a
 
      10-27-2004
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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-27-2004
"Noname" <(E-Mail Removed)> wrote in message
newseOfd.28816$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Arndt Jonasson
Guest
Posts: n/a
 
      10-27-2004

"Noname" <(E-Mail Removed)> writes:
> 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;
 
Reply With Quote
 
Noname
Guest
Posts: n/a
 
      10-27-2004
Paul Lalli wrote:

> "Noname" <(E-Mail Removed)> wrote in message
> newseOfd.28816$(E-Mail Removed)...
> > 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


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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-27-2004
"Noname" <(E-Mail Removed)> wrote in message
news:QPOfd.28817$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Arndt Jonasson
Guest
Posts: n/a
 
      10-27-2004

"Noname" <(E-Mail Removed)> writes:
> 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.
 
Reply With Quote
 
Thomas Kratz
Guest
Posts: n/a
 
      10-27-2004
Paul Lalli wrote:

> 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^.-
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-27-2004
Arndt Jonasson <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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:umper;
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:umper;
print Dumper \%hash;

__END__

Sinan
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-27-2004
A. Sinan Unur <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Arndt Jonasson <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
> > 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.


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
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-27-2004
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote in
news:clomen$b2a$(E-Mail Removed)-Berlin.DE:

> 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
 
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
std::map::find() throws exception when map is empty? Matthias Hildebrand C++ 5 03-20-2012 06:09 AM
I can map all files (.*) to asp.net worker.How do I map NO FILE to asp.net worker? alex ASP .Net 1 02-04-2005 03:18 AM
searching keys in std::map using map::upper_bound Erik Arner C++ 0 11-02-2004 11:14 PM
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM
map that maps to iterators in the same map ? Vlad C++ 0 12-15-2003 08:29 PM



Advertisments