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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I am attempting to search each line of each file in a directory for a specific string, and if found, grab a pertinent piece of data in the same line and then search for all the instances of that data and put them into another file. The files I'm searching are delimited by the pipe ("|") character. I am attempting to use split with the pipe as the delimiter but it seems to return each character into my array instead of each substring between pipes as if the delimiter is undefined. I am relatively new to PERL so I may be missing something quite obvious. My code is below...Any ideas?
#!/Usr/local/bin/perl; use Tie::File; use Getopt::Long; use warnings; #string to search for my($target1) = '6169600047'; #files and directories my($indir) = "c:/FAMD_EDI"; my($outdir) = "c:/FAMD_EDI"; my($outfile) = "FAMD_EDI_Master.txt"; open(OUTDIR, $outdir); open(OUTFILE,">$outdir/$outfile") or die "Cannot create output File"; opendir(MYDIR, $indir) or die "can't open dir $indir: $!"; while (defined($infile = readdir(MYDIR))) { next if $infile =~ /^\.\.?$/; open(FILEHNDL, "$indir/$infile") or die "Cannot open input file $!" +; while ($line = <FILEHNDL>) { chomp($line); #print "line= $line\n"; @cols = split(/|/, $line); if ($cols[11] = $target1) { #complete processing } } close(FILEHNDL); } close(OUTFILE); closedir(MYDIR);

In reply to using split by maurkb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (4)
    As of 2021-04-14 01:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?