473,748 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

map function with hashes .

51 New Member
hi ,
I have come across a code as below in the perldoc:

%hash = map { get_a_key_for($ _) => $_ } @array;

Could anybody tell me the meaning of the above line? please explain map function with reference to hashes. I have understood the meaning of map in general and have worked with an example. But i could not understand the meaning of the same with respect to hashes. So please help me.

Thanks & Regards,
pavan.
Jun 13 '08 #1
1 3849
KevinADC
4,059 Recognized Expert Specialist
map is nothing more than a loop, similar to a "for" or "foreach" loop, although there are differences. In your example it is just turning the array into a hash but applying some processing first before using the elements of the array as a hash key. Your code is the same as:

Expand|Select|Wrap|Line Numbers
  1. @array = (1..100);
  2. %hash = map {$_ => $_} @array;
  3. foreach my $key (keys %hash) {
  4.    print "The key is $key and the value is $hash{$key}\n";
  5. }
  6.  
The difference is that your code has a subroutine in the map block to preprocess the hash keys in some manner.

While the entire statement works in a right to left manner, within the map block, things work left to right. Here is a sort of flow chart using arrows and numbers to show the order and direction of processing:

Expand|Select|Wrap|Line Numbers
  1. output <--(3) map {(2)-->} <-- (1) input
the input ( could be an array or list or hash or anything that returns a list, like the grep function ) is read into the map function one element at a time, inside the map block each element is processed starting with the left most set of statements inside the map block and working to the right. When the map block is finished processing the elements it sends them to the output, which is a list of some kind, a hash or arrray most likely.

http://perldoc.perl.or g/functions/map.html
Jun 13 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
14188
by: Damien Morton | last post by:
Ive been doing some investigation of python hashtables and the hash function used in Python. It turns out, that when running pystone.py, as much time is spent in string_hash() as is spent in lookdict_string(). If you look at python's hash function, shown below, you can get an idea why - a multiply is used for every character in the string. static long string_hash1(register char *p, int size)
8
2748
by: Ben Holness | last post by:
Hi All, I want to create a hash array, based on values in a database. Basically, I want a hash array for each database key and I want to use a sub to get the hash array, but I am having a great deal of difficulty!! I have written an example script, taking out the DB side of things, to explain what I want to do and how I want to do it. I am obviously doing something wrong, but I don't know what :)
2
2855
by: MartyNg | last post by:
I am running a system that has both Classic ASP applications and a smattering of ASP.NET applications. We want to store passwords on a SQL Server table as their MD5 hashes. What is the safest way to get this hash value, and be able to verify it against user logins in both Classic ASP and ASP.NET? We have been working with the system.security.cryptopgraphy MD5 functions in .NET, and the functions here for Classic ASP...
2
7691
by: Bernard Dhooghe | last post by:
The information center writes: "Encryption Algorithm: The internal encryption algorithm used is RC2 block cipher with padding, the 128-bit secret key is derived from the password using a MD2 message digest. " and also explains how the length of the encrypted column can be derived.
6
1393
by: vamsinadella | last post by:
Hi all, I have to write a simple script (PHP or Perl) which takes in certain parameters, appends them and hashes them using SHA or SHA1 (or even MD5 is also ok) and spits out the hashed value. But the catch is, this script (may be sitting on a web server or something) has to be accessible by any application desired. some xyz app say written in .net or php or VB or any abc s/w should be able to access this script and get the hashed value. I'm...
23
2308
by: nsa.usa | last post by:
Hi, I used to use a function in other languages (TP or asm don't remember) where I could get number of clockticks since 1980. Is there a similar function in C? I don't seem to find it. I need to seed the random generator and since it's a web-server app, I can't seed with the date/time because the resolution is not great enough. If the app runs twice in the same second (or millisecond or whatever the resolution is) then the random...
11
2156
by: January Weiner | last post by:
Hello, I need to use a hashing function for relatively short strings (roughly 16 characters or less). The data that needs to be accessed via hash is just a simple int. I just need to look up values in two dimensional matrices each time that I encounter a pair of these strings. I would like to keep the code as simple and standard as possible, but at the same time to have a reasonable performance.
0
1326
by: HalfCoded | last post by:
hi everyone, I am kind of stuck and therefore would really appreciate some clues: I actually have to run a script which has to compare two elements from two different files which are a blast file and a cdf file I need also to keep the data structure For this I chose the following strategy: -dumping the files into two arrays -doing a pattern matching between the two files.
10
6556
by: aurekha | last post by:
Hi I have hashes with arrays to its keys like, %h1 = ('a'=>, 'b'=>, 'c'=> ); %h2 = ('a'=>, 'b'=>); then, how can i compare the 2 hashes(based on values. not keys) and get distinct values ?
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8247
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.