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:
(2) Questions regarding the [Hash] and [Reading a directory]

 



lie soul
Novice

Sep 25, 2012, 8:07 PM

Post #1 of 4 (1456 views)
(2) Questions regarding the [Hash] and [Reading a directory] Can't Post

I have two questions.

1) I'm trying to let the user type his "directory path" (perl example.pl /Users/name/file) in order to show the files or directory folder, i also wanted to show if the file or directory can or cannot be read, written, and executed


This the code i came up with.. but i still wanted to know if i can find the way of indicating if the file or directory and if it can or cannot be
read, written, and executed. I also wanted to make sure about what i making is good on other systems ( Linux/Unix/Mac)


Code
#!/usr/bin/perl -w 

use strict;

my $dir = shift || die "Argument missing: Please make sure from your directory name\n";

my $dir = "..";
opendir DIR, $dir;
my @values = readdir(DIR);
closedir DIR;

foreach (@values) {
printf "$_ is a file\n" if -f "$dir/$_";
printf "$_ is a dir\n" if -d "$dir/$_";
}



2) Question 2, i want to print out elements of hash and i want to use different ways of listing them.. just as each funiction, keys function,
values function and finaly sort keys.. and what i did was the following code and i don't know where is my problem...

$A = user input..
$B = user input..


Code
#!/usr/bin/perl -w 

use strict;

print "Enter A: ";
$A = <STDIN>;
chomp $A;
print "Enter B: ";
$B = <STDIN>;
chomp $B;

my %hash = (
'A' => "$A",
'B' => "$B",
'Z' => 'Z',
'D' => 'D',
'H' => 'H',
);

print "A: $hash{ 'A' }\n";
print "B: $hash{ 'B' }\n";
print "Z: $hash{ 'Z' }\n";
print "D: $hash{ 'D' }\n";
print "H: $hash{ 'H' }\n";



These are the 2 questions that i'm facing problem with...

Thanks


Laurent_R
Veteran / Moderator

Sep 25, 2012, 9:05 PM

Post #2 of 4 (1452 views)
Re: [lie soul] (2) Questions regarding the [Hash] and [Reading a directory] [In reply to] Can't Post

Hi,

1. Perl has a number of file test operators, mimicking to a certain extent the Unix file test operators.

Example:


Code
if (-e $file) { # ... 
}


This test for the existence of a file. There are a number of other such tests checking tor privileges and other read/write access, etc., check the documention to figure out exactly the one you need.

2. You don't state what your problem is.


lie soul
Novice

Sep 25, 2012, 11:05 PM

Post #3 of 4 (1448 views)
Re: [Laurent_R] (2) Questions regarding the [Hash] and [Reading a directory] [In reply to] Can't Post

Please note that i'm new to perl and i'm here to learn..

regarding my question 1: i didn't understand your point.. would you please be more clear about your example and what do you mean by doucomuntation.

for the second question.. i asked if i can list the output in different methods.. such as keys function,
values function and sort keys.. what i did with my code is that i used one way of listing and couldn't come up with different way by using the keys function,
values function and sort keys...

I hope this made it clear to you and thanks for your help...


FishMonger
Veteran / Moderator

Sep 26, 2012, 7:53 AM

Post #4 of 4 (1437 views)
Re: [lie soul] (2) Questions regarding the [Hash] and [Reading a directory] [In reply to] Can't Post

Perl comes with a complete set of documentation which can be accessed from the command line via the perldoc command. Or, you can access it online at http://perldoc.perl.org/

For Q1, you want to read up on the file test operators which is disgussed in perldoc perlfunc. http://perldoc.perl.org/functions/-X.html

For Q2, I'm not clear as to what you are needing to accomplish and since this appears to be your homework assignment, I hesitate to provide too much in the way of code.

The keys, values, and each functions are used in loops to access the data in the hash and the sort function is, obviously, used to sort the data. Please provide more details on what you need to accomplish and the actual code you used.

 
 


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

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