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

Re: Read in a file containing the criteria for a program when it runs

by brx (Pilgrim)
on Jul 18, 2012 at 16:40 UTC ( [id://982483]=note: print w/replies, xml ) Need Help??


in reply to Read in a file containing the criteria for a program when it runs

#!/usr/bin/env perl use strict; use warnings; my @filter; my $line = <DATA>; #open (my $ffile, '<','filterfile') or die "cannot open filterfile: $! +"; #my $line = <$ffile>; #while ($line = <$ffile>) { while ($line = <DATA>) { chomp $line; my ($col,$rel,$val,$foa,$ord) = split /\s+/,$line; $filter[$ord] = [ $col,$rel,$val,$foa ]; } for my $ord (1 .. 3) { print "$ord:\n", "\tcol: " ,$filter[$ord]->[0],"\n", "\trsh: " ,$filter[$ord]->[1],"\n", "\tvalue: " ,$filter[$ord]->[2],"\n", "\tf_o_a: " ,$filter[$ord]->[3],"\n\n"; } __DATA__ column relationship value filter_or_append order a <= 0.3 filter 1 b = abc append 3 c <= 0.3 filter 2

Each element of @filter is a reference to an anonymous array which contains values: [...].

Produces:

1: col: a rsh: <= value: 0.3 f_o_a: filter 2: col: c rsh: <= value: 0.3 f_o_a: filter 3: col: b rsh: = value: abc f_o_a: append

Replies are listed 'Best First'.
Re^2: Read in a file containing the criteria for a program when it runs
by dkhalfe (Acolyte) on Jul 18, 2012 at 17:22 UTC

    Awesome!! Quick question. Did you mean to comment out the three lines under my $line = <DATA>. If so, why? Thanks again. Much appreciated.

      In this example, I use __DATA__ section and read it with <DATA>. This way, all is included in one file.

      But you will probably use an external file. To do that, just delete my $line = <DATA>; and while ($line = <DATA>) { (and __DATA__ section). Then uncomment the three lines.

Re^2: Read in a file containing the criteria for a program when it runs
by BillKSmith (Monsignor) on Jul 18, 2012 at 16:55 UTC

    Excelent choice of data structure if $ord is always a small interger and there are few (if any) gaps in the sequence.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-03-29 05:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found