For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Sort by Hash









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sort by Hash
banker123

2006-11-05, 6:57 pm

I posted this question earlier, I did not explain it very well based on
the response received. Let me give it another try.

I have a loop that extracts data into the following variables from
several files (30-40).
$box
$jobname
$files

I have a hash
Key Value
1 123 ($box)
2 456 ($box)

I would like to sort the data using the hash key. The output would
look like this based on the sort defined in th hash.

$box $jobname $files
123 name1 file1
456 name2 file2

I will respond timley if further clarification is needed, please let me
know.

DJ Stunks

2006-11-05, 6:57 pm

banker123 wrote:
> I posted this question earlier, I did not explain it very well based on
> the response received. Let me give it another try.
>
> I have a loop that extracts data into the following variables from
> several files (30-40).
> $box
> $jobname
> $files
>
> I have a hash
> Key Value
> 1 123 ($box)
> 2 456 ($box)
>
> I would like to sort the data using the hash key. The output would
> look like this based on the sort defined in th hash.
>
> $box $jobname $files
> 123 name1 file1
> 456 name2 file2
>
> I will respond timley if further clarification is needed, please let me
> know.


here is the best way to get good responses to your questions on this,
or any other programming, forum:

1) post minimal complete code. this means including the strict and
warnings pragmas and ensure your code runs clean under both of these.
if it doesn't and you don't understand what's causing the error post
that. minimal means to boil your problem down into the smallest
possible scope. don't post 400 lines of perl that works fine because
you're having a problem with one loop.

2) include sample input data - the best way to do this is by using the
special DATA filehandle at the end of your code.

3) include an example of the desired output

if you follow these three instructions you will get good help
everytime.

-jp

banker123

2006-11-05, 6:57 pm

> 1) post minimal complete code.

foreach $file (@files) {
open (data, "$dir$file") or die "Cannot open file: $!";
while ( <data> ) {
if ( /JOBNAME/ ) {
$job = substr($_,8,8);
chomp($job);
$job=sprintf("%8s", $job);
$count++;
}
elsif ( /\.TIF\s{12}/ ) {
$box = substr($_,71,8);
}
#HELP NEEDED BELOW TRYING TO SORT OUTPUT BASED ON HASH

%priorities=('1','123','2','456');
foreach (sort keys %priorities) {
write;
last;

> 2) include sample input data

Hash is included in the code above, the variables change as the loop
executes, I woul like to sort the report using $box as defined in the
loop and the corresponding key in the hash.

> 3) include an example of the desired output


Sorted using the hash. (Box is the lookup value and key is the sort
order).
Box Jobname
123 Job1
456 Job2

Uri Guttman

2006-11-05, 6:57 pm

>>>>> "b" == banker123 <bradbrockman@yahoo.com> writes:
[color=darkred]
b> foreach $file (@files) {
b> open (data, "$dir$file") or die "Cannot open file: $!";

did you read the whole list of suggestions? where is the actual input
data? rewrite this to use the special DATA handle and put the data at
the end of the code after a line of __DATA__

b> while ( <data> ) {
b> if ( /JOBNAME/ ) {
b> $job = substr($_,8,8);
b> chomp($job);

why are you chomping something you just substr'ed? unless the input is
short, you wouldn't get a newline. if you care, then you should chomp
first as that is more common. but as the data is not shown nor specified
no one can help with that code.

b> $job=sprintf("%8s", $job);
b> $count++;
b> }
b> elsif ( /\.TIF\s{12}/ ) {
b> $box = substr($_,71,8);

it might be easier to check and gran the parts you want in one
regex. again, no way to tell without data. i sense a infinite loop here
in this reply.

b> }
b> #HELP NEEDED BELOW TRYING TO SORT OUTPUT BASED ON HASH

b> %priorities=('1','123','2','456');
b> foreach (sort keys %priorities) {
b> write;
b> last;
[color=darkred]
b> Hash is included in the code above, the variables change as the loop

i see no hash being stuffed with any DATA. i see poorly formatted blocks
and not one hash being loaded from the input loop.

b> executes, I woul like to sort the report using $box as defined in the
b> loop and the corresponding key in the hash.

but it will change behavior based on the DATA. you must show real
data. you can't assume we know what you know without DATA. i repeat,
SHOW SOME DATA.
[color=darkred]

b> Sorted using the hash. (Box is the lookup value and key is the sort
b> order).
b> Box Jobname
b> 123 Job1
b> 456 Job2

but where is the hash? you never build it. showing what it looks like is
irrelevent unless the code that stuffs it works and you haven't shown
that.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
DJ Stunks

2006-11-05, 6:57 pm


banker123 wrote:
>
> foreach $file (@files) {
> open (data, "$dir$file") or die "Cannot open file: $!";
> while ( <data> ) {
> if ( /JOBNAME/ ) {
> $job = substr($_,8,8);
> chomp($job);
> $job=sprintf("%8s", $job);
> $count++;
> }
> elsif ( /\.TIF\s{12}/ ) {
> $box = substr($_,71,8);
> }
> #HELP NEEDED BELOW TRYING TO SORT OUTPUT BASED ON HASH
>
> %priorities=('1','123','2','456');
> foreach (sort keys %priorities) {
> write;
> last;
>
> Hash is included in the code above, the variables change as the loop
> executes, I woul like to sort the report using $box as defined in the
> loop and the corresponding key in the hash.
>
>
> Sorted using the hash. (Box is the lookup value and key is the sort
> order).
> Box Jobname
> 123 Job1
> 456 Job2


This code is not complete. Complete code is code that I can copy and
paste into a file and run.

do you want help or not? help us help you. no one here is going to
pull teeth in order to help you meet your deadline.

you already received help where you weren't clear enough. he had to
guess at what you were doing and apparently guessed wrong.

paste something that goes from the shebang line up to __END__ and
includes DATA. print what you want to print unsorted and we can help
you sort it.

-jp

banker123

2006-11-05, 6:57 pm

> This code is not complete. Complete code is code that I can copy and
> paste into a file and run.


format STDOUT_TOP =

Data Entry To Complete

Lockbox Job File Batches
------------------------------------------------------------------
..


format STDOUT =
@<<<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<<<<<@<<<<<
$box $job, $file, $count
..


#Open directory
my $dir="G:/Formware/files/";
opendir DH, $dir or die "Cannot open$!";

#Get system date in the format mmddyy
@months = qw(01 02 03 04 05 06 07 08 09 10 11 12);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfW,
$dayOfYear, $daylightSavings) = localtime();
$dayOfMonth=sprintf("%02d", $dayOfMonth);
$year="06";
$date = "$months[$month]$dayOfMonth$year";

#Read each file beginning with system date from directory
@files=grep(/^$date/,readdir(DH));
closedir (DH);

foreach $file (@files) {
open (data, "$dir$file") or die "Cannot open file: $!";
while ( <data> ) {
if ( /JOBNAME/ ) {
$job = substr($_,8,8);
chomp($job);
$job=sprintf("%8s", $job);
$count++;
}
elsif ( /\.TIF\s{12}/ ) {
$box = substr($_,71,8);
write;
last;

}

}}

Uri Guttman

2006-11-05, 6:57 pm


i still see no input data. and that is not stripped down to the problem
of sorting a hash. remove all code except that which will stuff a hash
the way you want. dump the hash with Data::Dumper. show us the sort
order you want. you have yet to show any input data nor explain the
sort.

also check the faq which covers basic sorts like this. but it won't help
you learn how to show data or how to stuff a hash.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
banker123

2006-11-05, 6:57 pm

I am not sure how to "stuff" the hash this is where I need help along
with the sorting. What I have is the data extracted in scalar
variables at this point.

My input data ( I am stripping the filename, jobname and 8 digits after
the spaces into scalar variables).

Filename 110506_99999.BDF
JOBNAME=123N
111111111111111110614.TIF
111111111111111111111111111111
111111111111111110615.TIF
111111111111111111111111111111


Filename 110506_99999.BDF
JOBNAME=456N
111111111111111110614.TIF
22222222222222222222222222222 111111111111111110615.TIF
22222222222222222222222222222
111111111111111110616.TIF
22222222222222222222222222222

banker123

2006-11-06, 9:56 pm

I think the problem that I am running into is my data structure. I
have extracted the variables to a scalar variables and I think I need
to be extracting them to an array or hash. This is where I need the
help (bulding array or hash), we can then tackle the task of sorting
the data once in the correct structure.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com