Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Accessing Large Else-If Condition in a Separate Files

by neversaint (Deacon)
on Jun 05, 2007 at 04:05 UTC ( [id://619292]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Masters,

Currently I have following code. As you can see it has a very large else-if condition.

Is it possible to put this else-if condition in the separate file? (since they are going to be used for several other scripts). If it is possible, how can we do it?
#!/usr/bin/perl -w use strict; my $dataset = $ARGV[0] || 'foo1'; my @act_bsites; if ( $dataset eq 'foo1') { @act_bsites = ( '1 - 201 - 229', '2 - 201 - 229', '3 - 201 - 229', ); } elsif ( $dataset eq 'foo2') { @act_bsites = ( '1 - 201 - 218', '2 - 201 - 218', '3 - 201 - 218', '4 - 201 - 218', '5 - 201 - 218', '6 - 201 - 218', '7 - 201 - 218', ); } #..... goes on with 150 of such conditions (upto foo150). # do something with @act_bsites
Update: Thanks for the suggestions so far. I am aware that accessing with HOA can be done. But my problem is that the the generation of the @act_bsites coordinate took very long time, and I need to re-use them with other scripts. That's why I need to hardcode the else-if condition.

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Accessing Large Else-If Condition in a Separate Files
by Zaxo (Archbishop) on Jun 05, 2007 at 04:24 UTC

    You want a hash of arrays. That simplifies the logic and puts the data all in one place.

    my %data = ( foo1 => [ '1 - 201 - 229', '2 - 201 - 229', '3 - 201 - 229', ], foo2 => [ '1 - 201 - 218', '2 - 201 - 218', '3 - 201 - 218', '4 - 201 - 218', '5 - 201 - 218', '6 - 201 - 218', '7 - 201 - 218', ], # . . . );
    Then you can just say,
    my @act_bsites = @{$data{$dataset}};
    If the 1-150 ordering is natural and important, you may want to drop the 'foo' part of the designation and use an array instead of a hash.

    Update: I don't understand how hardcoding the data in an extended if-else construct improves anything with respect to hard-won data. Here's a version with a seperate data file:

    # file: /path/to/foosites { foo1 => [ '1 - 201 - 229', '2 - 201 - 229', '3 - 201 - 229', ], foo2 => [ '1 - 201 - 218', '2 - 201 - 218', '3 - 201 - 218', '4 - 201 - 218', '5 - 201 - 218', '6 - 201 - 218', '7 - 201 - 218', ], # . . . };
    and then in your code it can be as simple as,
    my @act_bsites = @{(do '/path/to/foosites')->{$dataset}};

    After Compline,
    Zaxo

Re: Accessing Large Else-If Condition in a Separate Files
by bobf (Monsignor) on Jun 05, 2007 at 04:20 UTC

    If you've got 150 if/elsif conditions I'd suggest taking a step back and rethinking the design. A dispatch table might be a more elegant solution.

    Secondly, I'm not sure how much your data in @act_bsites varies, but from the example you posted it looks like you could generate this on the fly with a few parameters. For example:

    use strict; use warnings; use Data::Dumper; my %foodata = ( 'foo1' => { thirdpart => 229, size => 3 }, 'foo2' => { thirdpart => 218, size => 7 } ); foreach my $foo qw( foo1 foo2 ) { my @act_bsites = map { join( ' - ', $_, 201, $foodata{$foo}{thirdpart +} ) } 1 .. $foodata{$foo}{size}; print Dumper( \@act_bsites ); }

    Finally, to actually answer your question, yes. You can put the data and/or the conditions into a separate file. See use, do, and eval (then there's the OO approach...).

    I apologize for being a bit on the vague side, but I'd rather understand the problem a little better before jumping in and recommending a complete solution.

    Update following the update to the OP:
    A database comes to mind, but that might be overkill. If so, I'd suggest putting the data into 150 separate files. Use a simple lookup mechanism (e.g., a hash) to determine the appropriate path/filename for a given input (e.g., 'foo1'), then use a single routine (which takes the path/filename as a parameter) to read in the data. If your data sets are small, you could put them all into one file, as GrandFather suggested.

Re: Accessing Large Else-If Condition in a Separate Files
by GrandFather (Saint) on Jun 05, 2007 at 04:25 UTC

    Either toss the data into a module or into an external file and use something like:

    use warnings; use strict; my $dataset = 'foo1'; my %site_data; my $site; while (<DATA>) { chomp; next unless length; if (/^(\w+):$/) { $site = $1; next; } push @{$site_data{$site}}, $_; } die "No site data for $dataset" unless exists $site_data{$dataset}; my @act_bsites = @{$site_data{$dataset}}; print join "\n", @act_bsites; __DATA__ foo1: 1 - 201 - 229 2 - 201 - 229 3 - 201 - 229 foo2: 1 - 201 - 218 2 - 201 - 218 3 - 201 - 218 4 - 201 - 218 5 - 201 - 218 6 - 201 - 218 7 - 201 - 218

    Prints:

    1 - 201 - 229 2 - 201 - 229 3 - 201 - 229

    to access it.


    DWIM is Perl's answer to Gödel
Re: Accessing Large Else-If Condition in a Separate Files
by GrandFather (Saint) on Jun 05, 2007 at 04:44 UTC

    In answer to the Update:

    so chuck the whole current file into the __DATA__ section and:

    local $/ = 'if ('; while (<DATA>) { chomp; next if ! length; next unless /eq '(\w+)'/; my $site = $1; push @{$site_data{$site}}, /'(\d+[^']+)'/gm; }

    to parse it.


    DWIM is Perl's answer to Gödel
      Why do something on your own if CPAN can do the same for you?

      Just store the Hash of Arrays in a serialized format (such as YAML, JSON, XML or even use Data::Dumper to write to a perl file) and you don't have to worry about parsing and writing theses lines by yourself

        Hi,
        That's interesting. Can you give example how this can be done with one of those modules?

        ---
        neversaint and everlastingly indebted.......
Re: Accessing Large Else-If Condition in a Separate Files
by TilRMan (Friar) on Jun 06, 2007 at 04:49 UTC
    If you absolutely must use Perl code instead of a datafile, you can use do (the do EXPR form) to execute another file, e.g., do "mydata.pl".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found