CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to join hash keys with data output?

 



hwnd
User

Jun 1, 2013, 9:37 AM

Post #1 of 7 (624 views)
How to join hash keys with data output? Can't Post

Say I have a file with the contents like this:


Code
title: foo 
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10
title: bar
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10


How can I push this to an empty hash with the data structure being something like this?


Code
foo-1 => 1 2 3 4 5 
foo-2 => 6 7 8 9 10
bar-1 => 1 2 3 4 5
bar-2 => 6 7 8 9 10



Zhris
Enthusiast

Jun 1, 2013, 1:07 PM

Post #2 of 7 (600 views)
Re: [hwnd] How to join hash keys with data output? [In reply to] Can't Post

Hi,

My attempt is highly dependent on input data order and format:


Code
#!/usr/bin/perl 
use strict;
use warnings;
use Data::Dumper;

my ( $data, $title, $id );

while ( my ( $key, $value ) = split /\:\s/, <DATA> )
{
$value =~ s/\s?\r?\n$//;

$key eq q/title/ ? $title = $value :
$key eq q/id/ ? $id = $value :
$key eq q/num/ ? $data->{qq/$title-$id/} = $value :
undef ;
}

print Dumper( $data );

__DATA__
title: foo
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10
title: bar
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10


Chris


(This post was edited by Zhris on Jun 1, 2013, 1:27 PM)


Laurent_R
Veteran / Moderator

Jun 1, 2013, 1:25 PM

Post #3 of 7 (594 views)
Re: [hwnd] How to join hash keys with data output? [In reply to] Can't Post

Hi,

try this:


Code
use strict; 
use warnings;
use Data::Dumper;
my ($key_start, $key);
my %data_hash;
while (<DATA>) {
chomp;
$key_start = $1 if /^title: (\w+)/;
$key = $key_start . "-" . $1 if /^id: (\d+)/;
$data_hash{$key} = $1 if /^num: ([\s\d]+)/;
}
print Dumper %data_hash;



__DATA__
title: foo
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10
title: bar
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10


This prints the following output:


Code
$ perl hash.pl 
$VAR1 = 'foo-1';
$VAR2 = '1 2 3 4 5 ';
$VAR3 = 'foo-2';
$VAR4 = '6 7 8 9 10 ';
$VAR5 = 'bar-1';
$VAR6 = '1 2 3 4 5 ';
$VAR7 = 'bar-2';
$VAR8 = '6 7 8 9 10';


UPDATE: I had not seen Chris had replied while I was typing my solution. Well, you've got two possible solutions now.


(This post was edited by Laurent_R on Jun 1, 2013, 1:28 PM)


hwnd
User

Jun 1, 2013, 1:41 PM

Post #4 of 7 (587 views)
Re: [Laurent_R] How to join hash keys with data output? [In reply to] Can't Post

And my attempt is similar to what your output is before I asked this question. To be clear, I am trying to store my data like this.


Code
$VAR1 = { 
'foo-1' => '1 2 3 4 5',
'foo-2' => '6 7 8 9 10',
'bar-1' => '1 2 3 4 5',
'bar-2' => '6 7 8 9 10',
};



2teez
Novice

Jun 1, 2013, 2:20 PM

Post #5 of 7 (582 views)
Re: [hwnd] How to join hash keys with data output? [In reply to] Can't Post

Have you tried the solution given by Zhris? I think it gives a type of output you want. However, if you don't mind you can toy with this also. Just the same concept.

Code
#!/usr/bin/perl 
use warnings;
use strict;

my %hash_data;
my $key;

while (<DATA>) {
s/\s+$//;
if (/title:\s+?(.+)/) {
$key = $1;
}
elsif (/id:\s+?(.+)/) {
$key .= '-' . $1;
}
elsif (/num:/) {
$hash_data{$key} = ( split /\w+:/, $_ )[1];
$key =~ s/-.+$//;
}

}
use Data::Dumper;
print Dumper \%hash_data;
__DATA__
title: foo
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10
title: bar
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10]

Output:

Code
$VAR1 = { 
'foo-1' => ' 1 2 3 4 5',
'foo-2' => ' 6 7 8 9 10',
'bar-1' => ' 1 2 3 4 5',
'bar-2' => ' 6 7 8 9 10'
};



(This post was edited by 2teez on Jun 1, 2013, 2:25 PM)


Laurent_R
Veteran / Moderator

Jun 2, 2013, 2:25 AM

Post #6 of 7 (558 views)
Re: [2teez] How to join hash keys with data output? [In reply to] Can't Post

In my solution, I just constructed a hash having the desired structure. If you want to print it the way you've shown, you only need to pass a ref to this hash to Dumper. In other words, change only the last line:


Code
print Dumper \%data_hash;


which will output:


Code
$ perl hash.pl 
$VAR1 = {
'foo-1' => '1 2 3 4 5 ',
'foo-2' => '6 7 8 9 10 ',
'bar-1' => '1 2 3 4 5 ',
'bar-2' => '6 7 8 9 10'
};


Or you could build directly a hash ref rather that a hash:


Code
use strict; 
use warnings;
use Data::Dumper;
my ($key_start, $key);
my $hash_ref;
while (<DATA>) {
chomp;
$key_start = $1 if /^title: (\w+)/;
$key = $key_start . "-" . $1 if /^id: (\d+)/;
$hash_ref->{$key} = $1 if /^num: ([\s\d]+)/;
}
print Dumper $hash_ref;

__DATA__
title: foo
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10
title: bar
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10


which gives the same output as just above.


Zhris
Enthusiast

Jun 5, 2013, 11:24 AM

Post #7 of 7 (531 views)
Re: [hwnd] How to join hash keys with data output? [In reply to] Can't Post

Data dumper individually dumps each element of @_. A hash is just an array, when read in array context it will read as key, val, key, val..., as seen by Laurent's dump. By passing in a reference to the hash instead, the data structure will be contained in a single dump.

Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives