Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

using less structures

by ovedpo15 (Pilgrim)
on Aug 28, 2019 at 13:32 UTC ( [id://11105165]=perlquestion: print w/replies, xml ) Need Help??

ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: using less structures
by Marshall (Canon) on Aug 28, 2019 at 18:17 UTC
    I agree with talexb, if you could make an example that is "runnable", this helps a lot. In the best case, I just copy and paste your code into my development editor and push the "go" button!

    Please do not delete anything from your current post. If say you even had a completely new formulation, just say UPDATE and post the revised code as an addition. Sometimes I download some code and then maybe some hours elapse before I actually post my reply. If you have removed what I'm replying to, that creates confusion.

    Ok, with limited understanding, I will post some general comments:

    Sounds like you had working code, then a new situation showed up - the possibility of getting more detailed directory info via a text file (the "file" key) instead of just higher level directory info via the "data" key. You responded by testing for this new condition in a couple of different places. This among other things has resulted in: 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.

    It appears to me that the goal is to create the @list_of_dirs array. In the "data" case, you have to do some searching to find the sub dirs and in the "file" case, you are going to believe what the file says about sub dirs without any searching.

    If the goal indeed is to create the @list_of_dirs array, consider refactoring the code to decide what situation you are in and just get on with the job, perhaps:

    my @list_of_dirs; # Create @list_of_dirs; if( defined($opt_href->{"data"}) ) { # We have to do some searching to find complete list of dirs @roots_paths = (split /,/, $opt_href->{"data"}); foreach $path (@root_paths) { find( sub { get_valid_dir( \@list_of_dirs) }, $path); } } else { # Directory info is in a file, just have to validate info is corre +ct blah...blah.. }
    I don't know for sure, but perhaps some intermediate variable "weirdness" will go away if the focus is upon creating the list_of_dirs. Something to consider is making this procedure a sub: my @list_of_dirs = get_dirs(...). Also, comments and whitespace consume ZERO mips! Those lines can often be some of the most important "code" lines that you write. Perhaps, even: # example "data" key is "...".

    You ignore what I probably would judge sufficient to cause a fatal error.
     open(my $fh, '<', "$file_path") or return 0; or
     next unless(isValidDir($fub_path));
    In general when dealing with a file, I assume that the "file is right". If there is a problem with the file, the "right" answer is often to fix the corrupted file and run the program again. I do make a distinction between a file generated by a program and a file generated by a human. The possible scenarios are many - more than I can detail here.

    I have a number of "micro comments" about specific lines. I don't think they are important now.

    Additional comment: I am not sure why you would prefer the "data" key over the "file" key? What happens if both keys are in the hash?

Re: using less structures
by talexb (Chancellor) on Aug 28, 2019 at 13:48 UTC

    It sounds like you have some code that's working that you'd like simplified. If you can reduce your code to a clear example of that, please update your post (don't delete anything -- just add).

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11105165]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-23 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found