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