Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Record Hash Data Structure (Newbie)

Reply
Thread Tools

Record Hash Data Structure (Newbie)

 
 
rajasekaran.natarajan@gmail.com
Guest
Posts: n/a
 
      02-14-2005
Hi all,

I am trying to write a program which extracts the data feom a text file
and stores it in a data structure. But there is some mistake and I
couldnt able to get over with.

I am a newbie (my 25th program or so) in Perl and I have checked the
oreilly books and perldoc but I have some limitations to understand to
be frank. (i mean unable to get the thing correct after reading)

below I had given my script and if anybody can thorw some light I will
be greatfull. Thanks in advance.

#!/usr/sbin/perl -w
#extracts grid data from test.dat and stores in dataStructure
$file1 = "test.dat";
open(CLIST,"$file1") || die ("Cant open input file \"$file1\", Unable
to Find, Check the file name and path");
while($line= <CLIST>){
if ($line =~ m/^GRID/) {
($gridNo,$cp,$xcor,$ycor,$coor,$cd)= unpack("x8 A8 A8 A8 A8
A8",$line);
$record = {
GRIDNO => $gridNo,
cp => $cp,
xcor => $xcor,
ycor => $ycoor,
zcor => $zcoor,
cd => $cd,
};
$grid{ $record->{GRIDNO} } = $record;
}
while( $gridNo = each %grid) {
printf "%s is grid no\n",$grid->{$gridNo};
}
}
close(CLIST);

 
Reply With Quote
 
 
 
 
chris-usenet@roaima.co.uk
Guest
Posts: n/a
 
      02-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am trying to write a program which extracts the data from a text file
> and stores it in a data structure. But there is some mistake and I
> couldn't able to get over with.


* Have you tried with "use strict", and explicitly declaring the
variables? If not, why not?
* What does your sample data look like?
* What are you expecting to happen?
* What actually happens?

Chris
 
Reply With Quote
 
 
 
 
rajasekaran.natarajan@gmail.com
Guest
Posts: n/a
 
      02-14-2005
I have got the mistake!

 
Reply With Quote
 
rajasekaran.natarajan@gmail.com
Guest
Posts: n/a
 
      02-14-2005
I have got the mistake!

 
Reply With Quote
 
ioneabu@yahoo.com
Guest
Posts: n/a
 
      02-14-2005

(E-Mail Removed) wrote:
> Hi all,
>
> I am trying to write a program which extracts the data feom a text

file
> and stores it in a data structure. But there is some mistake and I
> couldnt able to get over with.
>
> I am a newbie (my 25th program or so) in Perl and I have checked the
> oreilly books and perldoc but I have some limitations to understand

to
> be frank. (i mean unable to get the thing correct after reading)
>
> below I had given my script and if anybody can thorw some light I

will
> be greatfull. Thanks in advance.
>
> #!/usr/sbin/perl -w
> #extracts grid data from test.dat and stores in dataStructure
> $file1 = "test.dat";
> open(CLIST,"$file1") || die ("Cant open input file \"$file1\", Unable
> to Find, Check the file name and path");
> while($line= <CLIST>){
> if ($line =~ m/^GRID/) {
> ($gridNo,$cp,$xcor,$ycor,$coor,$cd)= unpack("x8 A8 A8 A8 A8
> A8",$line);
> $record = {
> GRIDNO => $gridNo,
> cp => $cp,
> xcor => $xcor,
> ycor => $ycoor,
> zcor => $zcoor,
> cd => $cd,
> };
> $grid{ $record->{GRIDNO} } = $record;
> }
> while( $gridNo = each %grid) {
> printf "%s is grid no\n",$grid->{$gridNo};
> }
> }
> close(CLIST);



I have no idea what your program is supposed to do and I think you have
some typos, but here is a version of your program that compiles with
perl -c program.pl. Clean it up and provide some test data. Learning
Perl is a great book to start with. I keep re-reading the first few
chapters and l learn something new every time.

#!/usr/bin/perl

use strict;
use warnings;

#extracts grid data from test.dat and stores in dataStructure
my $file1 = "test.dat";
open(CLIST,$file1) || die ("Cant open input file \"$file1\", Unable
to Find, Check the file name and path");
my $line;
my ($gridNo,$cp,$xcor,$ycor,$zcor,$cd);
my %grid;
while($line= <CLIST>){
if ($line =~ m/^GRID/) {
($gridNo,$cp,$xcor,$ycor,$zcor,$cd)= unpack("x8 A8 A8 A8 A8
A8",$line);
my $record = {
GRIDNO => $gridNo,
cp => $cp,
xcor => $xcor,
ycor => $ycor,
zcor => $zcor,
cd => $cd,
};
$grid{ $record->{GRIDNO} } = $record;
}
while( $gridNo = each %grid) {
printf "%s is grid no\n",$grid{$gridNo};
}

}

close(CLIST);

good luck!

wana

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      02-14-2005


(E-Mail Removed) wrote:

> my ($gridNo,$cp,$xcor,$ycor,$zcor,$cd);


> while($line= <CLIST>){
> if ($line =~ m/^GRID/) {
> ($gridNo,$cp,$xcor,$ycor,$zcor,$cd)= unpack("x8 A8 A8 A8 A8
> A8",$line);


You are suffering from a nasty case of premature declaration there. You
probably should get that seen to before it causes you too much
embarrassment.

Anyhow since you immediately puy this into a hash, a hash slice
assignment would be more natural.

@{my $record}{qw( GRIDNO cp xcor ycor zcor cd)} =
= unpack("x8 A8 A8 A8 A8 A8",$line);

 
Reply With Quote
 
ioneabu@yahoo.com
Guest
Posts: n/a
 
      02-14-2005

Brian McCauley wrote:

> You are suffering from a nasty case of premature declaration there.

You
> probably should get that seen to before it causes you too much
> embarrassment.
>
> Anyhow since you immediately puy this into a hash, a hash slice
> assignment would be more natural.
>
> @{my $record}{qw( GRIDNO cp xcor ycor zcor cd)} =
> = unpack("x8 A8 A8 A8 A8 A8",$line);


I agree. I was not trying to fix the structure of the OP's code. I
just corrected typos and did the minimal amount of changes possible to
make it compile. I should not have touched it without more information.

 
Reply With Quote
 
rajasekaran.natarajan@gmail.com
Guest
Posts: n/a
 
      02-15-2005
Hi all

Thanks a lot for your comments and help.
I just cant forgive myself for my typos I am sorry for that.
Here is the corrected version and it is working.

`Learning perl` I am using but they didnt give much about the hash of
hash etc.


I have also given the input data format. I need to link the generated
hash data to the element hash which I am gonna do later in the second
part. Basically every element has four grids and each grid has x,y,z
corordinates. + 2 more field. which kind of format is better to store
this and retrieve it for almost a 300,000 elements.

Any help will be appreciated.

ps: I tried to use `use strict` but it makes life harder for a newbie
like me. any better detailed source abt the use strict stuff.

#!/usr/sbin/perl -w
#extracts grid data from test.dat and stores in hash of hash
# raj - 15/Feb/2005

$file1 = "test.dat";
($identifier,$gridNo,$cp,$xcor,$ycor,$zcor,$cd);
open(DECK,"$file1") || die ("Cant open input file \"$file1\", Unable to
Find, Check the file name and path");
while($line= <CLIST>){
if ($line =~ m/^GRID/) {
($identifier,$gridNo,$cp,$xcor,$ycor,$zcor,$cd)= unpack("A8 A8 A8 A8
A8 A8 A8",$line);
%grid = (
$gridNo => {cp => $cp, xcor => $xcor, ycor => $ycor, zcor => $zcor,},
);
}
foreach $gridNo (keys %grid) {
printf
"$gridNo,$grid{$gridNo}->{xcor},$grid{$gridNo}->{ycor},$grid{$gridNo}->{zcor}\n";
}
}
close(DECK);

#Input File Format for test.dat <this is a MSC/NASTRAN INPUT DECK>
#each input field has 8 columns/fields
#COR-is co-ordinat value
#<name> <NUBMER> <CPVALUE> <XCOR> <YCOR> <ZCOR> <CPVALUE> each 8
columns
#GRID 130421 780.0 -422.625267.99
#GRID 130422 780.0 -380.25 267.99
#GRID 130423 780.0 380.25 10.73996
#GRID 130424 780.0 423.562510.73996
#GRID 130425 780.0 466.5 10.73996
#------------------------------------------------------
#NEXTPART
#<ElementName> <ElementNo> <Grid1> <Grid2> <Grid3> <Grid4>
#CQUAD4 115236 119524 119525 119528 119527
#CQUAD4 115237 119527 119528 119522 119521
#CQUAD4 115238 119521 119522 119519 119518

 
Reply With Quote
 
rajasekaran.natarajan@gmail.com
Guest
Posts: n/a
 
      02-15-2005
Hi all!
Yeah I included another hash for element data for the second part. but
I do not know how to acess the hash %grid using the values I get from
%element hash (check the print loop)

my $file1 = "test.dat";
my ($identifier,$gridNo,$cp,$xcor,$ycor,$zcor,$cd);
open(DECK,"$file1") || die ("Cant open input file \"$file1\", Unable to
Find, Check the file name and path");
while($line= <DECK>){
if ($line =~ m/^GRID/) {
($identifier,$gridNo,$cp,$xcor,$ycor,$zcor,$cd)= unpack("A8 A8 A8 A8
A8 A8 A8",$line);
%grid = (
$gridNo => {cp => $cp, xcor => $xcor, ycor => $ycor, zcor => $zcor,},
);
}
elsif($line =~ m/^CQUAD4/) {
($eId,$elementNo,$grid1,$grid2,$grid3,$grid4) = unpack("A8 A8 x8 A8
A8 A8 A8",$line);
%element = (
$elementNo => {ETYPE=> $eId, con1 => $grid1, con2 => $grid2, con3=>
$grid3, con4 => $grid4},
);
}
foreach $elementNo (keys %element) {
print
"\n$element{$elementNo}->{ETYPE},$elementNo,$element{$elementNo}->{con1},$element{$elementNo}->{con2},$element{$elementNo}->{con3},$element{$elementNo}->{con4}
\n";
#here I wanted to print grid1-grid4 (from `con1`-`con4` value of
%element) and its x,y,z corordinates (x,y,z needs tobe picked from like
%grid{con1}->{xcor} is it possible
#I dont know how to do that I tried many ways.
}
}
close(DECK);

My Present Goal is to print like this

Element1 grid1 grid2 grid3 grid4 (-> this line is ok)
grid1 xcor ycor zcor -> these four lines I could not get.
grid2 xcor ycor zcor
grid3 xcor ycor zcor
grid4 xcor ycor zcor
Element2 grid1 grid2 grid3 grid4
grid1 xcor ycor zcor
grid2 xcor ycor zcor
grid3 xcor ycor zcor
grid4 xcor ycor zcor

I tried something like that - don`t laugh - it did not work

foreach $elementNo (keys %element) {
print
"\n$element{$elementNo}->{ETYPE},$elementNo,$element{$elementNo}->{con1},$element{$elementNo}->{con2},$element{$elementNo}->{con3},$element{$elementNo}->{con4}
\n";
@g = ($element{$elementNo}->{con1},$element{$elementNo}->{con2},
$element{$elementNo}->{con3}, $element{$elementNo}->{con4});
foreach $g (@g) {
print "$g,$grid{$g}->{xcor},$grid{$g}->{ycor},$grid{$g}->{zcor}\n";

}

Is there anyother way to do this stuff more efficiently.
Any help will be apprciated.

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-15-2005
(E-Mail Removed) wrote:
>
> I have also given the input data format. I need to link the generated
> hash data to the element hash which I am gonna do later in the second
> part. Basically every element has four grids and each grid has x,y,z
> corordinates. + 2 more field. which kind of format is better to store
> this and retrieve it for almost a 300,000 elements.


If you try to store this as 300,000 hashes, you may run into memory
problems due the large amount of overhead with hashes. It depends on
how much memory you have, of course. I might be tempted to switch to
parallel hashes for each field, each with 300,000 entries. More annoying
to work with in some ways, but more memory efficient.

> ps: I tried to use `use strict` but it makes life harder for a newbie
> like me. any better detailed source abt the use strict stuff.


We were all newbies at one point, and trust us, "use strict" does not make
your life harder. It may seem that way at first, but remember that you
came here for our wisdom, and this is the centerpiece of that wisdom. Use
strict.

For detailed info, see perldoc strict, and the section on "my" in
perldoc perlfunc. And perldoc perlsub.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Simple structure and copying data to pointer of the same structure A C++ 27 04-16-2011 11:07 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Perh Hash of Hash of Array structure Shiraz Perl Misc 1 03-29-2007 07:47 PM
hash of hash and complex data structure ngoc Ruby 4 05-27-2005 08:54 AM



Advertisments