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!
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.
RonB 589
Recognized Expert Moderator Contributor
Here's one option. - #!/usr/bin/env perl
-
-
use strict;
-
use warnings;
-
use Data::Dumper;
-
-
my %cfg;
-
-
while (my $line = <DATA>) {
-
chomp $line;
-
my ($key, @values) = split /\|/, $line;
-
-
if (@values > 1) {
-
$cfg{$key} = \@values;
-
}
-
else {
-
$cfg{$key} = $values[0];
-
}
-
}
-
print Dumper \%cfg;
-
-
__DATA__
-
size|3
-
directory|c:\unicorn
-
sub-directories|sub|new
which outputs: -
$VAR1 = {
-
'sub-directories' => [
-
'sub',
-
'new'
-
],
-
'directory' => 'c:\\unicorn',
-
'size' => '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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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
|
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...
|
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...
| |
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...
|
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
|
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,...
|
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";
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |