MAP question

Discussion in 'Perl Misc' started by Noname, Oct 27, 2004.

  1. Noname

    Noname Guest

    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
     
    Noname, Oct 27, 2004
    #1
    1. Advertising

  2. Noname

    Paul Lalli Guest

    "Noname" <> wrote in message
    news:peOfd.28816$...
    > 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
     
    Paul Lalli, Oct 27, 2004
    #2
    1. Advertising

  3. "Noname" <> 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;
     
    Arndt Jonasson, Oct 27, 2004
    #3
  4. Noname

    Noname Guest

    Paul Lalli wrote:

    > "Noname" <> wrote in message
    > news:peOfd.28816$...
    > > 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
     
    Noname, Oct 27, 2004
    #4
  5. Noname

    Paul Lalli Guest

    "Noname" <> wrote in message
    news:QPOfd.28817$...
    > 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
     
    Paul Lalli, Oct 27, 2004
    #5
  6. "Noname" <> 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.
     
    Arndt Jonasson, Oct 27, 2004
    #6
  7. Noname

    Thomas Kratz Guest

    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^.-
     
    Thomas Kratz, Oct 27, 2004
    #7
  8. Arndt Jonasson <> wrote in
    news::

    > 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. Sinan Unur, Oct 27, 2004
    #8
  9. Noname

    Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > Arndt Jonasson <> wrote in
    > news::
    >
    > > 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
     
    Anno Siegel, Oct 27, 2004
    #9
  10. -berlin.de (Anno Siegel) wrote in
    news:clomen$b2a$-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
     
    A. Sinan Unur, Oct 28, 2004
    #10
  11. -berlin.de (Anno Siegel) writes:
    > A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > > Arndt Jonasson <> wrote in
    > > news::
    > >
    > > > 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} = ();


    This is much better, of course. Thanks for correcting me. (Going by mere
    impressions is of course not a good thing to do.)
     
    Arndt Jonasson, Oct 28, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex
    Replies:
    1
    Views:
    654
    Lau Lei Cheong
    Feb 4, 2005
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    7,992
    krogers
    Mar 20, 2012
  3. Vlad
    Replies:
    0
    Views:
    368
  4. Patrick Guio
    Replies:
    6
    Views:
    3,214
    chris
    Oct 20, 2004
  5. Erik Arner
    Replies:
    0
    Views:
    1,317
    Erik Arner
    Nov 2, 2004
Loading...

Share This Page