Contructing a dir. tree

Prabh

Usenet Archive
Joined
Jun 23, 2003
Messages
41
Reaction score
0
Hello all,
I'm given a path to an element,"foo/bar1/bar2/XYZ.txt" and I'm trying to print the following format from my input:

Parent Folder: foo
{
Some text here about "foo"
Child Folder: bar1
{
Some text here about "bar1"
Child Folder: bar2
{
Some text here about "bar2"
}
}
}

File element: Some text about "XYZ.txt"

I'm trying to do this as:

#!/usr/local/bin/perl

use strict ;
use warnings ;

my $input = 'foo/bar1/bar2/XYZ.txt' ;
my @arr = split(/\//,$input) ; # Get all folders.
my $finalIndex = $#arr ; # The final index of @arr. my $count = 0 ; my @out ; # Array stores the o/p format.

foreach my $line ( @arr )
{
# count == 0, parent folder.
if ( $count == 0 )
{
print "Parent Folder: $line \n { \n Some text here \n
} \n" ;
push(@out,"Parent Folder: $line \n { \n Some text here
\n }") ;

} elsif ( $count == $finalIndex ) # The file element, XYZ.txt { print "File element: $line\n" ; push(@out,"File element: $line") ; } else # All sub-directories here. { print "Sub-Folder: $line. Insert under $arr[$count - 1]\n" ; push(@out,"Sub-Folder: $line. Insert under $arr[$count
- 1]") ;
}

$count++ ;

}

print "======================================\n" ;
map { print "$_\n" ; } @out ;

It outputs,

Parent Folder: foo
{
Some text here
}
Sub-Folder: bar1. Insert under foo
Sub-Folder: bar2. Insert under bar1
File element: XYZ.txt

How do I insert the info about the sub-folder inside each other in a nested way and all of such info under the parent folder section?

Could anyone tell me how I go about doing it?

I tried looking up at splice, but it requires I give an offset to insert some elements inside my @out array.

Thanks for your time,
Prabh
 

Richard Gration

Usenet Archive
Joined
Jul 1, 2003
Messages
159
Reaction score
0
Hi

Have you considered recursion? Something like this (pseudocode-ish):

recurse_file('foo')

sub recurse_file {
filespec = arg1
if filespec is a directory
recurse_file(filespec)
else
do something with an ordinary file
fi
}

You could either print out stuff at each level or build a nested data structure depending on your needs.

More detail about your problem would help ... such as

1) where do you get your $input from
2) does it not make more sense to test the filesystem to see whether things are directories or files, rather than just looking at the position in a string based on an arbitrary path separator
3) are you interested in anything else in those directories
4) where does the "some text about ..." text come from

HTH
Rich
 

JR

Usenet Archive
Joined
Jun 13, 2001
Messages
485
Reaction score
0
Hello all,
I'm given a path to an element,"foo/bar1/bar2/XYZ.txt" and I'm trying to print the following format from my input:

Parent Folder: foo
{
Some text here about "foo"
Child Folder: bar1
{
Some text here about "bar1"
Child Folder: bar2
{
Some text here about "bar2"
}
}
}

File element: Some text about "XYZ.txt"

I'm trying to do this as:

#!/usr/local/bin/perl

use strict ;
use warnings ;

my $input = 'foo/bar1/bar2/XYZ.txt' ;
my @arr = split(/\//,$input) ; # Get all folders.
my $finalIndex = $#arr ; # The final index of @arr. my $count = 0 ; my @out ; # Array stores the o/p format.

foreach my $line ( @arr )
{
# count == 0, parent folder.
if ( $count == 0 )
{
print "Parent Folder: $line \n { \n Some text here \n
} \n" ;
push(@out,"Parent Folder: $line \n { \n Some text here
\n }") ;

} elsif ( $count == $finalIndex ) # The file element, XYZ.txt { print "File element: $line\n" ; push(@out,"File element: $line") ; } else # All sub-directories here. { print "Sub-Folder: $line. Insert under $arr[$count - 1]\n" ; push(@out,"Sub-Folder: $line. Insert under $arr[$count
- 1]") ;
}

$count++ ;

}

print "======================================\n" ;
map { print "$_\n" ; } @out ;

It outputs,

Parent Folder: foo
{
Some text here
}
Sub-Folder: bar1. Insert under foo
Sub-Folder: bar2. Insert under bar1
File element: XYZ.txt

How do I insert the info about the sub-folder inside each other in a nested way and all of such info under the parent folder section?

Could anyone tell me how I go about doing it?

I tried looking up at splice, but it requires I give an offset to insert some elements inside my @out array.

Thanks for your time,
Prabh

I'm not 100% sure I understand what you want, but try:

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

my $directory = 'foo/bar1/bar2/bar3/XYZ.txt';
my @arr = split /\//, $directory;
my @out = ;
my $info = undef;
my $count = 0;

$info = qq|Parent Folder: $_\n{\n Some text here about "$arr[0]"\n|; $info .= qq| Child Folder: bar1\n|; $info .= qq| {\n|;

my $indent = 6;
for my $i (1..$#arr-1) {
$info .= " " x $indent . qq|Some text here about "$arr[$i]"\n|; $info .= " " x $indent . qq|Child folder: $arr[$i+1]\n|; $info .= " " x $indent . qq|{\n|; $indent += 3; $count++; } $info .= " " x ($indent -= 3) . qq|}\n| for (0..$count+1);

$info .= qq|\nFile element: Some text about "$arr[$#arr]"\n|; $info .= "=" x 39, "\n"; print $info;

Good luck
 

Paul Lalli

Usenet Archive
Joined
Jan 3, 2004
Messages
2,926
Reaction score
0
I'm not 100% sure I understand what you want, but try:

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

use warnings;
my $directory = 'foo/bar1/bar2/bar3/XYZ.txt';
my @arr = split /\//, $directory;
my @out = ;

Why are you declaring a variable you never use?
my $info = undef;

That's a completely worthless initialization.
my $info;
does the same thing.
my $count = 0;

That's an almost-completely worthless initialization
my $count;
will have the same effect sine all you're ever doing to count is incrementing it. (An undef value increments to 1 without so much as a warning).
$info = qq|Parent Folder: $_\n{\n Some text here about "$arr[0]"\n|; $info .= qq| Child Folder: bar1\n|; $info .= qq| {\n|;

my $indent = 6;
for my $i (1..$#arr-1) {
$info .= " " x $indent . qq|Some text here about "$arr[$i]"\n|; $info .= " " x $indent . qq|Child folder: $arr[$i+1]\n|; $info .= " " x $indent . qq|{\n|; $indent += 3; $count++; } $info .= " " x ($indent -= 3) . qq|}\n| for (0..$count+1);

$info .= qq|\nFile element: Some text about "$arr[$#arr]"\n|; $info .= "=" x 39, "\n"; print $info;

I'm not at all clear as to why you're building up this (potentially huge) scalar to be printed only once at the end. Why are you not just printing each line of text as it comes? Can you explain what benefit is to be gained by storing all this text in memory before printing?

Paul Lalli
 

JR

Usenet Archive
Joined
Jun 13, 2001
Messages
485
Reaction score
0
Paul Lalli said:
use warnings;

Why are you declaring a variable you never use?

You're right-it is unneccesary. God help us. I was originally going to use an array, the way the o/p did, but instead decided to use a scalar.
That's a completely worthless initialization.
my $info;
does the same thing.

I know it does. It doesn't hurt anything either, to set it to undef; in fact, it makes is completely clear to even the novice programmer that it is, in fact, undefined.
That's an almost-completely worthless initialization
my $count;

I know dude. The reason I set it to zero is for the same reason I set $info to undef. Christ!
will have the same effect sine all you're ever doing to count is incrementing it. (An undef value increments to 1 without so much as a warning).
$info = qq|Parent Folder: $_\n{\n Some text here about "$arr[0]"\n|; $info .= qq| Child Folder: bar1\n|; $info .= qq| {\n|;

my $indent = 6;
$info .= qq|\nFile element: Some text about "$arr[$#arr]"\n|; $info .= "=" x 39, "\n"; print $info;

I'm not at all clear as to why you're building up this (potentially huge)

Potentially huge? I wonder just how huge a directory path is likely to be. Hmm, now I'm nitpicking just the way you have been. Sorry about that.
scalar to be printed only once at the end. Why are you not just printing each line of text as it comes? Can you explain what benefit is to be gained by storing all this text in memory before printing?

Paul Lalli

The o/p used an array to capture the information; I used a scalar. Perhaps he needs to use the scalar or array, whichever, later in the program. I realize he could just print the thing, instead of storing it and then printing it! I'm just throwing out a script to the o/p that he can either use or not, one that resebmles to some degree his original script.

Rather than lambaste my script, why don't you show us what you have, in the way of a solution? That would be a far more useful way for you to spend your time. You are clearly a vastly superior programmer, so put your talents to use.
 

Uri Guttman

Usenet Archive
Joined
Dec 16, 2000
Messages
3,126
Reaction score
0
JR> I know it does. It doesn't hurt anything either, to set it to undef; JR> in fact, it makes is completely clear to even the novice programmer JR> that it is, in fact, undefined.

you don't code for newbies, you code for regular programmers. if we all coded for newbies, it would be an ugly world.

JR> I know dude. The reason I set it to zero is for the same reason I set JR> $info to undef. Christ!

$DEITY!! don't you get it? redundant code like that initialization is bad. as in why do it as you gain nothing from it? do you realize how many places perl does things like that and if you explicitly coded them, i shudder to think how bad the code would be.

JR> Rather than lambaste my script, why don't you show us what you JR> have, in the way of a solution? That would be a far more useful JR> way for you to spend your time. You are clearly a vastly superior JR> programmer, so put your talents to use.

he did put his talents to use in trying to educate you. why don't you try to put your talents to use by learning from him how to improve your code.

uri
 

Paul Lalli

Usenet Archive
Joined
Jan 3, 2004
Messages
2,926
Reaction score
0
I know it does. It doesn't hurt anything either, to set it to undef; in fact, it makes is completely clear to even the novice programmer that it is, in fact, undefined.

I respectfully disagree. The 'hurt' this can cause is to lead a novice Perl programmer to think that variables must be initialized, that they are not undef by nature.
I know dude. The reason I set it to zero is for the same reason I set $info to undef. Christ!

And now you seem to be getting upset without cause. If you are not willing to have your programs critiqued, allow me to suggest this is perhaps not the best forum for you to post your programs, whether they are originals or solutions to another poster's problems.

Again, while *you* may know the initialization is not needed, a novice programmer may not. Especially one who comes from a C background where the initialization is needed. That is why I sent my post, for the learners. Not every reply to your message is intended soley for you.
Welcome to usenet.
$info = qq|Parent Folder: $_\n{\n Some text here about "$arr[0]"\n|; $info .= qq| Child Folder: bar1\n|; $info .= qq| {\n|;

my $indent = 6;
$info .= qq|\nFile element: Some text about "$arr[$#arr]"\n|; $info .= "=" x 39, "\n"; print $info;

I'm not at all clear as to why you're building up this (potentially huge)

Potentially huge? I wonder just how huge a directory path is likely to be. Hmm, now I'm nitpicking just the way you have been. Sorry about that.

Do you have any idea what the OP meant by "Some text here"? For all we know, he could be printing the Gutenberg Bible for every directory level. So yes, I think "potentially huge" is a valid description.
The o/p used an array to capture the information; I used a scalar. Perhaps he needs to use the scalar or array, whichever, later in the program. I realize he could just print the thing, instead of storing it and then printing it! I'm just throwing out a script to the o/p that he can either use or not, one that resebmles to some degree his original script.

My comments were not about the OP's post, nor about how well your post answered his question. I was simply wondering if there was any reason you made the design decision of storing all text in memory before printing it. There is no sarcasm in that sentence, btw. I am always willing to believe there are reasons for things I don't understand - this was a case of my asking if you had such a reason.
Rather than lambaste my script, why don't you show us what you have, in the way of a solution? That would be a far more useful way for you to spend your time. You are clearly a vastly superior programmer, so put your talents to use.

Again, welcome to usenet. I was not 'lambasting' your script, I was critiquing it and asking for further explanation. This is a community. The community is best served when many people make their comments and opinions known. The idea that any solution posted should just be left out in the world without any critiques is a poor one at best. Code should be critiqued and evaluated no matter where it comes from, nor why. That is how to best serve both the OP as well as any one else reading who may have similar questions.

Paul Lalli
 

JR

Usenet Archive
Joined
Jun 13, 2001
Messages
485
Reaction score
0
JR> Rather than lambaste my script, why don't you show us what you
JR> have, in the way of a solution? That would be a far more useful JR> way for you to spend your time. You are clearly a vastly superior JR> programmer, so put your talents to use.
he did put his talents to use in trying to educate you. why don't you try to put your talents to use by learning from him how to improve your code.

Educate me? Haven't I said that I know exactly to what he was referring with his comments? There is no education necessary. I was only trying to help the o/p. The code given did address the o/p's question. Sure, almost all code can be re-written more succinctly-I don't argue that. Don't people often just put code out there in newsgroups, without making it as succinct as possible, to address a given person's questions as quickly as possible? Surely, this isn't the first time. If the initialization of several variables unnecessarily is such a bad thing, I'll make sure to email any o/p about a possible solution in the future, rather than throwing out there some code for everyone to critique.

BTW-If I were coding a script such as the one wanted by the o/p, for my purposes, I wouldn't have initialized the variables. I tried to explain that I thought it would just make things more clear to the o/p. If not, well then, that's too bad, I guess. I was only trying to help.
 

Joe Smith

Usenet Archive
Joined
Feb 9, 2000
Messages
1,614
Reaction score
0
Paul said:
That's an almost-completely worthless initialization

But not completely worthless.
It's something that should be allowed without criticism.
-Joe
 

Anno Siegel

Usenet Archive
Joined
Jul 14, 2000
Messages
3,309
Reaction score
0
Joe Smith said:
But not completely worthless.
It's something that should be allowed without criticism.

The initialization is unnecessary, in the given context it makes absolutely no difference.

In Perl, the need to initialize a variable is an exception, so it tells the reader something when it's done. Unnecessary initializations make the program less clear than it could be.

Anno
 

ctcgag

Usenet Archive
Joined
Aug 1, 2002
Messages
297
Reaction score
0
Paul Lalli said:
That's an almost-completely worthless initialization
my $count;
will have the same effect sine all you're ever doing to count is incrementing it. (An undef value increments to 1 without so much as a warning).

If all you ever do with count is increment it, then
the variable itself is useless, regardless of how it is initialized.

Xho
 

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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,243
Members
248,133
Latest member
DujonT

Latest Threads

Top