How to create hash dynamically in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create hash dynamically in perl?
# 1  
Old 06-25-2010
How to create hash dynamically in perl?

Hi,

I have one file name file.txt

It has the following contents:

Code:
#File Contents

StartTime,EndTime,COUNTER1,COUNTER2,COUNTER3
12:13,12:14,0,1,0


The output should be like this:

Code:
StartTime: 12:13
ENDTIME:  12:14
COUNTER1: 0
COUNTER2: 1
COUNTER3: 0

I have to create hash dynamically.

How can i create a hash dynamically and get the above output?

I tried in Google but could not get satisfactory result.

Regards
Vanitha
# 2  
Old 06-25-2010
I don't know much about perl hashes (yet), but one could also solve that problem like this:
Code:
$ perl -lne '{@x=split(/,/,$_), $a=1 if /^Start/;
              @y=split(/,/,$_) if $a == 1}
              END{ for (0..4) {print "$x[$_]: $y[$_]"}}' file
StartTime: 12:13
EndTime: 12:14
COUNTER1: 0
COUNTER2: 1
COUNTER3: 0
$

# 3  
Old 06-25-2010
Maybe not the most efficient algorithm, but for most applications it should do:
Code:
$ cat hash.pl
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my $line;
my %hash;
$line = <DATA>;
chomp $line;
my @keys = split /,/, $line;
while ( $line = <DATA> ) {
    chomp $line;
    my @val = split /,/, $line;
    for ( my $i = 0 ; $i <= $#val ; $i++ ) {
        push @{ $hash{ $keys[$i] } }, $val[$i];
    }
}

print Data::Dumper->Dump( [ \%hash ], [qw/hash/] );

__DATA__
StartTime,EndTime,COUNTER1,COUNTER2,COUNTER3
12:13,12:14,0,1,0
$
$
$
$ perl hash.pl
$hash = {
          'COUNTER3' => [
                          '0'
                        ],
          'COUNTER1' => [
                          '0'
                        ],
          'COUNTER2' => [
                          '1'
                        ],
          'EndTime' => [
                         '12:14'
                       ],
          'StartTime' => [
                           '12:13'
                         ]
        };

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Dynamically parse BibTeX and create hash of hash

Hello gurus, Iam trying to parse following BibTex file (bibliography.bib): @book{Lee2000a, abstract = {Abstract goes here}, author = {Lee, Wenke and Stolfo, Salvatore J}, title = {{Data mining approaches for intrusion detection}}, year = {2000} } @article{Forrest1996, abstract =... (0 Replies)
Discussion started by: wakatana
0 Replies

3. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

4. Programming

perl script to create hash.

Hi, I have the xml file file this, perl script to create hash<p> <university> <name>svu</name> <location>ravru</location> <branch> <electronics> <student name="xxx" number="12"> <semester number="1"subjects="7" rank="2"/> </student> <student name="xxx"... (1 Reply)
Discussion started by: veerubiji
1 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

How to dynamically create user name in perl and move the files?

Hi, I have two problems in which i have stuck up. a) To move the files and copy back. b) To create user dynamically. Question 1: I have to move the files to some other directory and copy it when all the process is done. I have the code like this: $file = "/home/data/file1.txt";... (2 Replies)
Discussion started by: vanitham
2 Replies

7. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

8. Shell Programming and Scripting

Dynamically create arrays

I have a bash challenge. I have several virtualization hosts for which I need to collect VM information. I need to dynamically create an array for each host. The contents of the array should be the list VMs running on the host. Here's the logic; I just need someone to help me with the details:... (0 Replies)
Discussion started by: EdgarTorres
0 Replies

9. Shell Programming and Scripting

Need to dynamically create a file

I'm trying to write a script that generates a file of varying size filled with random text and am having some difficulties. The major problems are that I'm limited to csh and a lot of the systems I have to run this on don't have a /dev/random (so a wrapper around the dd if=/dev/random or dd... (14 Replies)
Discussion started by: stareja
14 Replies

10. Shell Programming and Scripting

How to set dynamically keys names in perl %hash

hello I have loop , in this loop im picking names , this names I want to be keys in %hash but I don't know how to set in every loop entertain different key in the %hash (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question