Hello,
I am trying to assign key/value pairs to a hash from a text input file.
I am
able to split the text up and assign it to variables and then try and assign
it to my hash, but only the last key/value pair ends up in the hash once the
the foreach loop has finished. I think it may be something to do with the
way that I am assigning the key/value pairs to the hash, and possibly due to
scope as well, but am unable to find out what I am doing wrong. Perhaps some
kind person on the list could assist me. Excuse my code which is probably
execrable. Thanks for any help.
Allister
#!/usr/bin/perl -w
open QUESTIN , "<questions" or die " \nCan't open file: $!";
my @quest_temp;
my %qnums_quests;
while (<QUESTIN>){
chomp;
push @quest_temp, $_ ;
}
foreach $quest_string (@quest_temp) {
my ($quest_number, $quest_number2, $quest_number3);
$quest_number = substr( $quest_string, 0, 3);
if ($quest_number =~ /[\d]{3}/) {
%qnums_quests = ($quest_number, $quest_string);
}
elsif ($quest_number =~ /[\d]{2}[\D]/) {
$quest_number2 = substr($quest_number, 0, 2);
%qnums_quests = ($quest_number2, $quest_string);
}
elsif ($quest_number =~ /[\d][\D]{2}/) {
$quest_number3 = substr($quest_number, 0, 1);
%qnums_quests = ($quest_number3, $quest_string);
}
else {
print "\n ERROR. This question is not formatted properly. This
question
number, \"$quest_number\", does not contain any digits.\n";
}
}
foreach $key (sort keys %qnums_quests) {
print "\n$key => $qnums_quests{$key}\n";
}
>>this produces;
10 => 10. Carolyn has a text file named 'guest_list' containing 12 lines. She
executes the following command. What is the result? Select one.<<# split -4
guest_list gl>><>a. The first four columns in the text are written to new
files glaa, glab, glac and glad.<>b. The first four columns in the text are
written to new files aagl, abgl, acgl and adgl.<>c. The lines of guest_list
are evenly divided among new files glaa, glab, glac and glad.<>d. The lines
of guest list are evenly divided among new files glaa, glab and glac.<>e. The
lines of guest_list are evenly divided among new files aagl, abgl and acgl.
>>END OF OUTPUT
The source text file is attached.
--
Public Key to be found at www.keyserver.net.
Search for [EMAIL PROTECTED]
1. What section of the online users manual and command reference holds
administrative (not user) commands? Select one.<>a. Section 1.<>b. Section
2.<>c. Section 8.<>d. Section n.<>e. Section s.
2. What two commands will display the status of processes on a Linux system?
Select one.<>a. ls and df.<>b. ps and top.<>c. ps and df.<>d. df and top.<>e.
du and df.
3. What does the device file /dev/hdb6 represent? Select one.<>a. An extended
partition on s SCSI disk drive.<>b. A logical partition on a SCSI disk
drive.<>c. An extended partition on an IDE disk drive.<>d. A primary partition
on an IDE disk drive.<>e. A logical partion on an IDE disk drive.
4. Which command will display the last lines of the text file 'file 1'? Select
one.<>a. head -b file1.<>b. head --bottom file1.<>c. head -v file1.<>d. tail
file1.<>e. tail -n 1 file1.
5. In the bash shell entering the !! command has the same effect as which one
of the following? Select one.<>a. Ctrl-P and Enter.<>b. Ctrl-N and Enter.<>c.
Ctrl-U and Enter.<>d. ! -2.<>e. !2.
6. Which of the following commands can be used to check an ext2 filesystem?
Select all that apply.<>a. fsck -ext2 /dev/hda5.<>b. fsck /dev/hda5.<>c. e2fsck
/dev/hda5.<>d. fsck.ext2 /dev/hda5.<>e. fsck.linux /dev/hda5.
7. Name the command that displays pages from the online user's manual and
command reference.
8. In response to the 'df' command the system reports a 'Use%' of '98%' for the
following filesystem mounted on '/home'. Which of the following best describes
the significance of this information?<>a. Files on /home are consuming 98% of
the physical disk.<>b. File read/write activity on /home are consuming 98% of
the system I/O capacity.<>c. Files on /home are consuming 98% of the /home
filesystem.<>d. Inodes on /home are nearly exhausted.<>e. Inodes on /home are
98% free.
9. Of the following directories, which is the most important to back up on a
regular basis?<>a. /var.<>b. /tmp.<>c. /usr.<>d. /root.<>e. /etc.
10. Carolyn has a text file named 'guest_list' containing 12 lines. She
executes the following command. What is the result? Select one.<<# split -4
guest_list gl>><>a. The first four columns in the text are written to new files
glaa, glab, glac and glad.<>b. The first four columns in the text are written
to new files aagl, abgl, acgl and adgl.<>c. The lines of guest_list are evenly
divided among new files glaa, glab, glac and glad.<>d. The lines of guest list
are evenly divided among new files glaa, glab and glac.<>e. The lines of
guest_list are evenly divided among new files aagl, abgl and acgl.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>