Stuff a hash from two source files

B

banker123

I am trying to populate a hash from two source files. The code below
only populates the last elements from the source files. I woul like to
populate the hash with all the elements foiund in the source files.

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

while ( <data1> ) {
($box,$priority)=split;
}
while ( <data2> ) {
($lockbox,$group)=split;
}

my %data = ( $box => { group => "$group" , priority => "$priority"} );

data1
712480 3
712481 2
712482 1

data2
712480 firstgreen
712481 firstblack
712482 firstblue
 
P

Paul Lalli

banker123 said:
I am trying to populate a hash from two source files. The code below
only populates the last elements from the source files.

That's all you told it to do.
I woul like to
populate the hash with all the elements foiund in the source files.

!/usr/bin/perl
use strict;

Please don't lie to us just to avoid us yelling about not using strict
and warnings. Your code does not compile with strict enabled. Post
your real code.
use warnings;

while ( <data1> ) {

There is no such filehandle.
($box,$priority)=split;

No such variables were ever declared.
}
while ( <data2> ) {

There is no such filehandle.
($lockbox,$group)=split;

No such variables were ever declared.
}

my %data = ( $box => { group => "$group" , priority => "$priority"} );

perldoc -q quoting


In addition to all the compilation problems, your central problem is
that your algorithm makes no sense. You read in all lines from one
file, overwriting the contents of two variables each time through.
Then you read in all lines from another file, overwriting the contents
of two other variables each time through. Then you finally declare a
hash, and populate it with exactly one key/value pair, that uses three
of those variables - which of course have only the last values read in
for each.

Step one: *actually* enable strict and warnings.
Step two: declare your hash before opening any files.
Step three: figure out what you actually want to make this hash look
like. You've given no information of any kind that would tell us how
the lines in one file relate to the lines in the other file, so there's
no way of knowing how you want the structure to end up.

Paul Lalli
 
J

John Bokma

banker123 said:
I am trying to populate a hash from two source files. The code below
only populates the last elements from the source files. I woul like to
populate the hash with all the elements foiund in the source files.

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

while ( <data1> ) {
($box,$priority)=split;
}
while ( <data2> ) {
($lockbox,$group)=split;
}

my %data = ( $box => { group => "$group" , priority => "$priority"} );

Post real code, not something you typed in, untested:

"use" not allowed in expression at C:\Documents and Settings\John\My
Documents\use.pl line 2, at end of line
syntax error at C:\Documents and Settings\John\My Documents\use.pl line
2, near "perl
use strict"
BEGIN not safe after errors--compilation aborted at C:\Documents and
Settings\John\My Documents\use.pl line 3.

Even if I consider the first line a copy mistake, and add the # I get:


Global symbol "$box" requires explicit package name at C:\Documents and
Settings\John\My Documents\use.pl line 6.
Global symbol "$priority" requires explicit package name at C:\Documents
and Settings\John\My Documents\use.pl line 6.
Bareword "data1" not allowed while "strict subs" in use at C:\Documents
and Settings\John\My Documents\use.pl line 8.
Global symbol "$lockbox" requires explicit package name at C:\Documents
and Settings\John\My Documents\use.pl line 9.
Global symbol "$group" requires explicit package name at C:\Documents
and Settings\John\My Documents\use.pl line 9.
Bareword "data2" not allowed while "strict subs" in use at C:\Documents
and Settings\John\My Documents\use.pl line 12.
Global symbol "$box" requires explicit package name at C:\Documents and
Settings\John\My Documents\use.pl line 12.
Global symbol "$group" requires explicit package name at C:\Documents
and Settings\John\My Documents\use.pl line 12.
Global symbol "$priority" requires explicit package name at C:\Documents
and Settings\John\My Documents\use.pl line 12.
Bareword "group" not allowed while "strict subs" in use at C:\Documents
and Settings\John\My Documents\use.pl line 12.
Bareword "priority" not allowed while "strict subs" in use at C:
\Documents and Settings\John\My Documents\use.pl line 12
..


Maye you thought that just manually adding use strict and use warnings
made things right?

Post real code.

Finally, how can you expect to get each item if you don't store the data
while you loop over your data?
 
B

banker123

My actual code.

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

open ('info', 'C:/sort.txt') or die "Cannot open file: $!";
open ('box', 'C:/box.txt') or die "Cannot open file: $!";

my %data;
while ( <info> ) {
($box,$prior)=split;
}
while ( <box> ) {
($lockbox,$group)=split;
}
$data {$box} = { group => $group , prior => $prior} ;

for my $table ( sort by_type keys %data ) {
my @data = ($table, @{ $data{$table} } {qw/group prior/});
print "@data\n";
}

sub by_type {
$data{$a}{prior} <=> $data{$b}{prior};
}
 
P

Paul Lalli

banker123 said:
My actual code.

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

This is plugging your fingers in your ears and shouting "LA LA LA I
CAN'T HEAR YOU!!!!". If you don't want Perl to tell you what you're
doing wrong, why are you bothering to ask us what you did wrong?
open ('info', 'C:/sort.txt') or die "Cannot open file: $!";
open ('box', 'C:/box.txt') or die "Cannot open file: $!";

my %data;
while ( <info> ) {
($box,$prior)=split;
}
while ( <box> ) {
($lockbox,$group)=split;
}
$data {$box} = { group => $group , prior => $prior} ;

So you didn't bother to listen to the suggestions that either John or I
gave you? Why bother asking us?

Good bye.
Paul Lalli
 
B

banker123

This is plugging your fingers in your ears and shouting "LA LA LA I
CAN'T HEAR YOU!!!!". If you don't want Perl to tell you what you're
doing wrong, why are you bothering to ask us what you did wrong?

Same result, few changes made after turning on strict and warnings as
suggested, and declaring the hash.

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

open ('info', 'C:/sort.txt') or die "Cannot open file: $!";
open ('box', 'C:/box.txt') or die "Cannot open file: $!";

my %data;
while ( <info> ) {
my ($box,$prior)=split;

while ( <box> ) {
my ($lockbox,$group)=split;
$data {$box} = { group => $group , prior => $prior} ;
}
}

for my $table ( sort by_type keys %data ) {
my @data = ($table, @{ $data{$table} } {qw/group prior/});
print "@data\n";
}

sub by_type {
$data{$a}{prior} <=> $data{$b}{prior};
}
 
J

John Bokma

banker123 said:
Same result, few changes made after turning on strict and warnings as
suggested, and declaring the hash.

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

open ('info', 'C:/sort.txt') or die "Cannot open file: $!";
open ('box', 'C:/box.txt') or die "Cannot open file: $!";

my %data;
while ( <info> ) {
my ($box,$prior)=split;

while ( <box> ) {
my ($lockbox,$group)=split;
$data {$box} = { group => $group , prior => $prior} ;
}
}

for my $table ( sort by_type keys %data ) {
my @data = ($table, @{ $data{$table} } {qw/group prior/});
print "@data\n";
}

sub by_type {
$data{$a}{prior} <=> $data{$b}{prior};
}


Cannot open file: No such file or directory at C:\Documents and Settings
\John\My Documents\foo.pl line 5.

perldoc -f open

Also, if you report an error, it might be useful to provide some
additional info, e.g.

my $filename = ....
.... or die "Can't open '$filename' for reading: $!";

^ errmesg
^ ^
what file action that failed

You got one out of 3.

(And if your files are really directly on drive C: ... aargh)
 
D

DJ Stunks

banker123 said:
#!/usr/bin/perl
use strict;
use warnings;

open ('info', 'C:/sort.txt') or die "Cannot open file: $!";
open ('box', 'C:/box.txt') or die "Cannot open file: $!";

my %data;
while ( <info> ) {
my ($box,$prior)=split;

while ( <box> ) {
my ($lockbox,$group)=split;
$data {$box} = { group => $group , prior => $prior} ;
}
}

for my $table ( sort by_type keys %data ) {
my @data = ($table, @{ $data{$table} } {qw/group prior/});
print "@data\n";
}

sub by_type {
$data{$a}{prior} <=> $data{$b}{prior};
}

okay, now we're getting somewhere. code that compiles.

what you're doing above is going through every line of <box> for each
line of <info>. what I think you want to be doing is building the
'prior' key value pair as you go through the info file, then adding the
'group' key value pair to the hash for each box as you go through each
line of <box>.

something like this: (untested)

my %data;
while (<info>) {
($box, $prior) = split;
$data{$box}{prior} = $prior;
}

while (<box>) {
($box,$group) = split;
$data{$box}{group} = $group;
}

-jp
 
B

banker123

DJ said:
what you're doing above is going through every line of <box> for each
line of <info>. what I think you want to be doing is building the
'prior' key value pair as you go through the info file, then adding the
'group' key value pair to the hash for each box as you go through each
line of <box>.
Exactly!

something like this: (untested)

my %data;
while (<info>) {
($box, $prior) = split;
$data{$box}{prior} = $prior;
}

while (<box>) {
($box,$group) = split;
$data{$box}{group} = $group;

Worked great, thanks!
 
A

Alan_C

okay, now we're getting somewhere. code that compiles.
something like this: (untested)
($box,$group) = split;
$data{$box}{group} = $group;

al@AB60R:~$ tst_hash
712482 firstblue 1
712481 firstblack 2
712480 firstgreen 3

I see that this removes dupes, appends each single digit to each appropriate
line, sorted by the single digits. Do I have that right? (my output is above,
there)

I understand the part that I did in the below code. But I do not understand
each code line below that I've appended an #?? onto.

I seek any sort of help that would in turn help me to understand these lines
that I do not grasp (explanations, terminologies of what for me to study,
perldocs, etc.) Thanks.


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

# open ('INFO', 'C:/sort.txt') or die "Cannot open file: $!";
# open ('BOX', 'C:/box.txt') or die "Cannot open file: $!";

############ important notice #################
# where <INFO> be/is the first 3 lines of DATA
# and <BOX> be/is the last 3 lines of DATA
my %data;
my @box_items; # gather_for_BOX
my ($box, $prior, $group);
# while (<INFO>) {
my $i = 0; # gather_for_BOX
while (<DATA>) {
########## gather_for_BOX ############
$i++;
if ($i > 3) {
push @box_items, $_;
next;
}
########### end of gather_for_BOX ###########
($box, $prior) = split;
$data{$box}{prior} = $prior; #??
}
# print @box_items;

# while (<BOX>) {
foreach ( @box_items ) {
($box,$group) = split;
$data{$box}{group} = $group; #??
}

for my $table ( sort by_type( keys %data )) {
my @data = ($table, @{ $data{$table} } {qw/group prior/}); #??
print "@data\n";
}

sub by_type {
$data{$a}{prior} <=> $data{$b}{prior}; #??
}
__DATA__
712480 3
712481 2
712482 1
712480 firstgreen
712481 firstblack
712482 firstblue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,658
Latest member
jaguar32

Latest Threads

Top