473,750 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hash or hash's of hash?

1 New Member
Hello,

I am a beginner in Perl. I am trying to read and then process a pipe delimited file. My file looks something like this-

size|3
directory|c:\un icorn
sub-directories|sub |new.....

The examples I have seen online show code where it is known how many columns the file consists. However, this is going to be a configuration file and number of columns in sub-directories is unknown.

My question is what do I need has or hash's of hash or something else in this case?

Thanks!
Jun 29 '12 #1
3 1736
RonB
589 Recognized Expert Moderator Contributor
You can use a hash, or a hash of arrays, or a hash of hashes. You haven't provided enough info for use to be able to make a clear recommendation.

Is the file format set in stone i.e., is it being dictated by your instructor/boss? If you can, I'd recommend changing it to a more standard config file format and possibly use one of the config modules on cpan to parse it.
Jun 29 '12 #2
RonB
589 Recognized Expert Moderator Contributor
Here's one option.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. my %cfg;
  8.  
  9. while (my $line = <DATA>) {
  10.     chomp $line;
  11.     my ($key, @values) = split /\|/, $line;
  12.  
  13.     if (@values > 1) {
  14.         $cfg{$key} = \@values;
  15.     }
  16.     else {
  17.         $cfg{$key} = $values[0];
  18.     }
  19. }
  20. print Dumper \%cfg;
  21.  
  22. __DATA__
  23. size|3
  24. directory|c:\unicorn
  25. sub-directories|sub|new
which outputs:
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = {
  2.           'sub-directories' => [
  3.                                  'sub',
  4.                                  'new'
  5.                                ],
  6.           'directory' => 'c:\\unicorn',
  7.           'size' => '3'
  8.         };
  9.  
Jun 29 '12 #3
numberwhun
3,509 Recognized Expert Moderator Specialist
If I may add to this, take a look at this link. I have used it as a reference for hashes in Perl and it has been extremely handy. Hopefully it helps.

Regards,

Jeff
Jul 2 '12 #4

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

Similar topics

3
4175
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own HashFcn and EqualKey template args) or, 2. convert the two unsigned ints to char*s, concatenate them and use that as Key. For method 1, the difficulty I am having is in writing the HashFcn. HashFcn requires the following method
24
4307
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to these keys -- I only need to be able to retrieve the value associated with a certain key, so I do not want to have the keys stored in memory. Could I just hash() the url strings first and use the resulting integer as the key? I think what I'm...
12
7016
by: Arash Partow | last post by:
Hi all, I've ported various hash functions to python if anyone is interested: def RSHash(key): a = 378551 b = 63689 hash = 0
21
3222
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
8
1801
by: pieceofcake | last post by:
Dear all, I am new to Perl. I have a large matrix. The easiest way to describe is it is like a calender where there are days along the top, numbers of weeks down the side, and the numbers of dates in the rest of the table. Imagine this calendar to be populated with different data but with the same concept, but more like 10,000s across and 10,000s down. How would I parse this huge dataset into a hash. I know how to do the smaller...
23
5741
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList or Array, correct? An example: create the object, create an array, the stuff the object into the array. Later on, assume the object is mutable, the object changes, but you can find it, if you have enough state information to uniquely identify...
7
2244
by: cckramer | last post by:
I have this info' in a file. blockA/input1 to block A/output1 delayXX blockA/input1 to blockA/output2 delayYY blockA/input1 to blockA/output3 delayZZ blockB/input1 to blockB/output1 delayPP blockC/input1 to blockC/output1 delayQQ blockC/input2 to blockC/output1 delayRR
2
3552
eWish
by: eWish | last post by:
I am in need of a good tutorial that explains taking the keys of one hash and the elements in a HOA and making them a HOH. I have a file that has is pipe delimited with 374 fields per line and 7.500 plus lines per file. I have about five of these files. I need to get this into a database and did not want to do it manually, Preserving the order is crucial. I am using Tie::Hash::Indexed to keep the order. Basically,...
1
2974
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my $i=0; $i<@input; $i++){ my $hash = $input; print "$i: \n";
24
44637
Rabbit
by: Rabbit | last post by:
INTRODUCTION The Secure Hash Algorithm 2 is a series of cryptographic hash algorithms designed by the US National Security Agency (NSA) and published by the National Institute of Standards and Technology (NIST) as a government standard. There is currently a competition being held by NIST to find a new family of algorithms for what will be named SHA-3. These new functions may not necessarily be derived from the SHA-2 algorithms. One of the...
0
8838
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
9583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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
9342
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
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8263
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...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.