sfrisch@mt.gov has asked for the wisdom of the Perl Monks concerning the following question:

I have a flat file I want to pull certain fields from the text and assign them to a variable I would like to know a strait forward method. I had thought to use split and substr? or load the line into an array the file is space delimited. So the field after HOST field6 I need to assign field seven as a variable and so on as I go across the line "$_" example

orclNetDescString DESCRIPTION ADDRESS PROTOCOL TCP HOST doa.sdasd.sdf.sdf. PORT 1521 CONNECT_DATA SID devdb GLOBAL_NAME devdb.discoveringmorclSid devdb

Replies are listed 'Best First'.
Re: Variable assignments flat file
by NetWallah (Canon) on Nov 04, 2015 at 03:21 UTC
    If you are looking for certain fields in the line(s) of text, this code will do the job.
    use strict; use warnings; my @lookfor = qw|PROTOCOL HOST PORT SID GLOBAL_NAME|; my $search_regex = "(" . join( "|", @lookfor) . ")"; my %item; my $inputstr = <DATA>; chomp $inputstr; while ($inputstr =~/$search_regex\s+(\S+)/g){ $item{$1} = $2; } print qq|$_ = $item{$_}\n| for sort keys %item; __DATA__ orclNetDescString DESCRIPTION ADDRESS PROTOCOL TCP HOST doa.sdasd.sdf. +sdf. PORT 1521 CONNECT_DATA SID devdb GLOBAL_NAME devdb.discoveringmo +rclSid devdb
    OUTPUT:
    GLOBAL_NAME = devdb.discoveringmorclSid HOST = doa.sdasd.sdf.sdf. PORT = 1521 PROTOCOL = TCP SID = devdb

            “The sources of quotes found on the internet are not always reliable.” — Abraham Lincoln.3; cf.

Re: Variable assignments flat file
by GrandFather (Saint) on Nov 04, 2015 at 00:28 UTC

    Show us some sample data. Show us what you've tried. Show us what you want out and what you get from your current code.

    See I know what I mean. Why don't you? for hints about how to include sample data in your sample script.

    Premature optimization is the root of all job security
      open FILE, 'c:\ldap_entries.txt'; while(<FILE>){ chomp; s/ //g; s/:/ /g; s/\(/ /g; s/\)/ /g; s/=/ /g; if(/orclNetDescString/){ $AA = $_; #print "$AA"; } if(/PORT/){ $AA1 = $_; } if(/orclSid/){ $AA2 = $_; # print "\n$AA$AA1$AA2\n"; $AA3 = "\n$AA$AA1$AA2"; push @LINE,$AA3; $server = $LINE[6]; $port1 = $LINE[8]; # Iwant to create TNSNames.txt based on a formatt # #alstrn = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = ( +PROTOCOL = TCP)(HOST = 10.194.146.184)(PORT = 1542))) (CONNECT_DATA = + (SERVICE_NAME = alstrn))) #outt put to file $SID = (DESCRIPTION = (ADDRESS_LIST = + (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port1))) (CONN +ECT_DATA = (SERVICE_NAME = $SID))) # } }

        Please always wrap your code and data within <code></code> tags per my message. To further, since this information is directly related to your original post, it would be best if you edited your original question to include it, so that new readers don't have to scroll the entire thread to find it. See How do I change/delete my post?.

Re: Variable assignments flat file
by Anonymous Monk on Nov 04, 2015 at 00:21 UTC

    split or m//atch or unpack ( if fixed width )