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:
store an array of data, use it in recognization

 



JJTAN
Novice

Sep 10, 2009, 1:45 AM

Post #1 of 2 (280 views)
store an array of data, use it in recognization Can't Post

my problem now is extracting data from report...

the reports sounds like that:

animal: 'dog'
height = 0.50
weight = 20

animal: 'cat'
height = 0.25
weight = 15

animal: 'bird'
height = 0.10
weight = 1.5

...
...
...
...

ends

using my coding below:

Code
if ($_=~/animal: '/) 
{
$animal1 = "$'";
if ($animal1 =~/\'/)
{
$animal2 = "$`";
}
}


how i can extract the height and weight out?

gurus outside please help me...


toolic
User

Sep 10, 2009, 6:42 AM

Post #2 of 2 (259 views)
Re: [JJTAN] store an array of data, use it in recognization [In reply to] Can't Post

One way to do it is to parse your input and
store it into a Hash-of-Hashes data structure.
Then you will be able to extract whatever
information you need.


Code
 
use strict;
use warnings;
use Data::Dumper;

my %animals;
my $animal;
while (<DATA>) {
chomp;
next unless /\S/;
if (/animal:\s+'(\w+)'/) {
$animal = $1;
next;
}
if (/=/) {
my ($param, $value) = split /\s+=\s+/;
$animals{$animal}{$param} = $value;
}
}

print Dumper(\%animals);

__DATA__
animal: 'dog'
height = 0.50
weight = 20

animal: 'cat'
height = 0.25
weight = 15

animal: 'bird'
height = 0.10
weight = 1.5


This prints out:


Code
$VAR1 = { 
'cat' => {
'weight' => '15',
'height' => '0.25'
},
'dog' => {
'weight' => '20',
'height' => '0.50'
},
'bird' => {
'weight' => '1.5',
'height' => '0.10'
}
};


 
 


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

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