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:
String of code wont run properly, did a -c and the syntax was okay

 



yoplocheo
Novice

Jan 10, 2016, 10:33 PM

Post #1 of 8 (1583 views)
String of code wont run properly, did a -c and the syntax was okay Can't Post


Code
# Perl by example, exercise 5 qn 3 
use warnings;
my (%courses);
%courses = (
"2CPR2B" => "C Language",
"1UNX1B" => "Intro to UNIX",
"3SH414" => "Shell Programming",
"4PL400" => "Perl Programming",
);

sub values_sort {
$courses{$a} cmp $courses{$b};
}
foreach my $values(sort values_sort (values %courses)) {
printf"\t%-20s%20s\n", $values, $courses{$values};
}

The errors pointed me to lines 13 and 15, but im not sure how to resolve them, really appreciate any help! Thankss.


Laurent_R
Veteran / Moderator

Jan 10, 2016, 11:32 PM

Post #2 of 8 (1580 views)
Re: [yoplocheo] String of code wont run properly, did a -c and the syntax was okay [In reply to] Can't Post

Hi,
I can't test right now on my mobile device, but it would be easier to help you if you included in your post the error messages that were displayed.


BillKSmith
Veteran

Jan 11, 2016, 6:16 AM

Post #3 of 8 (1577 views)
Re: [yoplocheo] String of code wont run properly, did a -c and the syntax was okay [In reply to] Can't Post

Your code would "work" if you use the function "keys" instead of "values".

Code
#foreach my $values(sort values_sort (values %courses)) {  
foreach my $values(sort values_sort (keys %courses)) {

Note that your function values_sort would sort the <b>keys</b> in the order of the <b>values</b>.

For each course, the script will print the course number followed by the course name. Courses will be arranged in order of course names.

This change will make your variable name "$values" very misleading. Change it to "$key".

Although it would not have helped in this case, always use strict;
Good Luck,
Bill


yoplocheo
Novice

Jan 11, 2016, 9:34 PM

Post #4 of 8 (1566 views)
Re: [BillKSmith] String of code wont run properly, did a -c and the syntax was okay [In reply to] Can't Post

Hi,
My intentions were actl to sort the values in alphabetical order. Why is it wrong to call out the 'values' function? How would the syntax of the code appear to be like then? Thanks a lot!


yoplocheo
Novice

Jan 11, 2016, 9:37 PM

Post #5 of 8 (1565 views)
Re: [BillKSmith] String of code wont run properly, did a -c and the syntax was okay [In reply to] Can't Post

This tutorial book i have taught me how to sort by keys alphabetically and numerically, and by values numerically. But there was no examples on how to sort by values alphabetically, so i tried to improvise!! :/


BillKSmith
Veteran

Jan 12, 2016, 6:42 AM

Post #6 of 8 (1554 views)
Re: [yoplocheo] String of code wont run properly, did a -c and the syntax was okay [In reply to] Can't Post

Did you try my suggestion? It does exactly what you intend.Smile

If you did not need to print the keys, you could sort the values directly:


Code
use strict; 
use warnings;
my %courses = (
"2CPR2B" => "C Language",
"1UNX1B" => "Intro to UNIX",
"3SH414" => "Shell Programming",
"4PL400" => "Perl Programming",
);
foreach my $value (sort values(%courses)){
print $value, "\n";
}


OUTPUT:

Code
C Language 
Intro to UNIX
Perl Programming
Shell Programming

There is no way to get the corresponding keys.

In order to print both, you must sort the keys. You sort them such that the corresponding values are in alphabetical order. This is exactly what your original function "values_sort" does if you use it to sort the keys. You print the keys with corresponding values in this order.

Here is what I intended (in my previous post) for you to do yourself:

Code
# Perl by example, exercise 5 qn 3  
use warnings;
use strict;
my (%courses);
%courses = (
"2CPR2B" => "C Language",
"1UNX1B" => "Intro to UNIX",
"3SH414" => "Shell Programming",
"4PL400" => "Perl Programming",
);

sub values_sort {
$courses{$a} cmp $courses{$b};
}
foreach my $key(sort values_sort (keys %courses)) {
printf"\t%-20s%-20s\n", $key, $courses{$key};
}

OUTPUT:

Code
        2CPR2B              C Language 
1UNX1B Intro to UNIX
4PL400 Perl Programming
3SH414 Shell Programming

Good Luck,
Bill


yoplocheo
Novice

Jan 12, 2016, 7:32 AM

Post #7 of 8 (1552 views)
Re: [BillKSmith] String of code wont run properly, did a -c and the syntax was okay [In reply to] Can't Post

OHHH I see I see. Also, correct me if im wrong, is this:

Code
$courses{$a}

and

Code
$courses{$b}

actually refering to the values of the hash?

Anyway I have gotten that result, thanks alot btw!


BillKSmith
Veteran

Jan 12, 2016, 10:49 AM

Post #8 of 8 (1548 views)
Re: [yoplocheo] String of code wont run properly, did a -c and the syntax was okay [In reply to] Can't Post

Exactly! $courses{$a} works the same as $courses{$key} in the printf statement. The variables $a and $b each contain an element of the array you are sorting (in this case keys).
Good Luck,
Bill

 
 


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

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