Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store data in Hash (Biggner)

Status
Not open for further replies.

BornToFly

Programmer
Joined
May 19, 2007
Messages
3
Location
DE
Hi,

I am a new program in Perl. I wrote a program that i find not so nice (Because i needed something fast).

I would like to store some data:

Name FileIndex Time JD
ABRA 1 02:326:00000 28345984
CACH 1 02:338:00000 34325435
ABRA 2 05:165:00000 56454766
ABRA 3 07:054:00000 73450985

only FileIndex and JD are a scalars and the others are strings. I wrote just 2 different names, but there could be more. This data o read from a different files and after that i would like to sort the data first by Name and then by JD.

My question is in which way should i store my data in order to use the powerful tool of hash in Perl? Because i would like to do some query if for instance ABRA exists more then once or not...

Thanks for your help
 
Might be worth considering a database such as MySql for your data storage. Perl and MySql work well together and make complex queries very simple to perform. You can use flat files but the data is hard to manipulate if you need anything other that very simple searches.

Keith
 
My question is in which way should i store my data in order to use the powerful tool of hash in Perl?

Store it just like you have it posted in your message. Or store it as a comma seperated file or using some other delimiter.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Maybe i was not clear, the data is stored in an ASCII file and i read it from there. What i am interesting are the 4 fields (Name, FileIndex, Time and JD). This data i use in order to generate new ASCII files. I would like to know how should i store it in Perl in order to be able to do sorting by name and then by JD and also to be able to to ask if i have more then one JD for the same name field.

My problem with Hashes was that from what i read, usually its come with pairs, key and value, but i could have, theoretically, more then one value corresponding to the same name.

Thank you
 
Something like this then maybe:

Code:
[url=http://perldoc.perl.org/functions/readline.html][black][b]readline[/b][/black][/url] <DATA>[red];[/red] [gray][i]#skip header[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%hash[/blue] = [red]([/red][red])[/red][red];[/red]
[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red][red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
   [black][b]my[/b][/black][red]([/red][blue]$n[/blue],[blue]$f[/blue],[blue]$t[/blue],[blue]$j[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][red])[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$hash[/blue][red]{[/red][blue]$n[/blue][red]}[/red][red]}[/red],[red][[/red][blue]$f[/blue],[blue]$t[/blue],[blue]$j[/blue][red]][/red][red];[/red] 
[red]}[/red]      
	

[teal]__DATA__[/teal]
[teal]Name    FileIn88dex    Time            JD[/teal]
[teal]ABRA       1         02:326:00000   28345984[/teal]
[teal]CACH       1         02:338:00000   34325435[/teal]
[teal]ABRA       2         05:165:00000   56454766[/teal]
[teal]ABRA       3         07:054:00000   73450985[/teal]

this will create a hash of arrays. So the hash key "ABRA" will store the values:

1,02:326:00000,28345984
2,05:165:00000,56454766
3,07:054:00000,73450985

each in it's own annonymous array. You can quickly tell if a "key" has more than one value by checking the length of the array that is the values of the keys. This will also enable you to sort the data easy enough by any of the fields/columns.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi,

Thanks to KevinADC, that is what i was searching for. I checked it on some test file and it works. My new problem is that it doesnt really works on the real data. I tried to change my test program to be with the same imputs like the real one, everything is file.

I added here the relevants parts:

Code:
#!/usr/bin/perl -w
#use warnings;
#use diagnostics;

use Class::Struct;
use Socket;
use Switch;
use File::Glob qw(:globally :nocase);

my @stations_data;
my %hash = ();

if ( !  open( DATA_SINEX  , "<" , $file_list[$i] ) ) {
                        die "How did you get logged in? ($!)";
} else {
        while ( $line_sinex = <DATA_SINEX> ) {
              chomp( $line_sinex );
              my @solution_epoch = split(/\s+/, $line_sinex);
              push( @{ $hash{ $solution_epoch[3] } } , ( "1" , $solution_epoch[6] , "0" ) );
        }
}

print "Size of a hash: " . keys( %hash ) . "\n";

The print out i get at the end is Zero. I know @solution_epoch is not empty, because the old part of the program works (and they are not empty).

Thanks
 
where does $i get defined?

Code:
$file_list[[red]$i[/red]]


here you used parenthesis () instead of [];

Code:
push( @{ $hash{ $solution_epoch[3] } } , [red]([/red] "1" , $solution_epoch[6] , "0" [red])[/red] );

sould be:

Code:
push( @{ $hash{ $solution_epoch[3] } } , [red][[/red] "1" , $solution_epoch[6] , "0" [red]][/red] );

post a few lines of the real data.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this will print a list of the hash keys:

Code:
print "Size of a hash: " . keys( %hash ) . "\n";

not how many how keys there are. If you want the number of hash keys you have to use scalar context:

Code:
print "Size of a hash: ", scalar keys( %hash ), "\n";

but that is not the size of the arrays, that is the number of hash keys.




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top