Contructing a dir. tree

P

Prabh

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
 
R

Richard Gration

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
 
J

JR

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
 
P

Paul Lalli

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
 
J

JR

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.
 
U

Uri Guttman

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
 
P

Paul Lalli

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
 
J

JR

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.
 
J

Joe Smith

Paul said:
That's an almost-completely worthless initialization

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

Anno Siegel

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
 
C

ctcgag

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
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top