I wrote the following code:
if(defined($opt_href->{"data"})) {
@roots_paths = (split /,/, $opt_href->{"data"});
} else {
my $file_path = $opt_href->{"file"};
open(my $fh, '<', "$file_path") or return 0;
while (my $row = <$fh>) {
chomp($row);
if ($row =~ /(.*),(.*)/) {
my ($main,$sub) = ($1,$2);
my $fub_path = $main."/".$sub;
next unless(isValidDir($fub_path));
push(@{$paths{$main}},$fub_path);
push(@roots_paths,$main);
}
}
close ($fh);
}
foreach my $main (sort(uniq(@roots_paths))) {
if(defined($opt_href->{"data"})) {
find( sub { get_valid_dir( \@list_of_dirs, $_ ) }, $root_path)
+;
} else {
@list_of_dirs = @{$paths{$main}};
}
# More same code here...
}
Before that, It only worked for
$opt_href->{"data"}. Now I'm trying to add
$opt_href->{"file"}.
In case of
$opt_href->{"data"} I just split a string of paths in format: ~main~,~main~,...,~main~ and for each main I find its subs.
For
$opt_href->{"file"} I get a file where each line is in format of ~main~,~sub~ and in that case I don't need to find them.
In the first case I use the array
@roots_paths and in the second case I have to use two structures
%paths and
@roots_paths.
I'm looking for a way to simplify it, I don't like the additional push
push(@roots_paths,$main); I have to do. Is there a way to achieve it? Somehow using the same structure?
Hope my question makes sense.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.