Without the use of a database, I am trying to find a better way than what I have to search a large CSV file. I got the size of it down to about 10mb, 221,000 lines. I'm trying to pull records and put stuff in the middle, as you can see, but I just know there is a much better way to do exactly what this does. Well, anyway, here's my code:


#!/usr/bin/perl -w my $employee_file = "employee_list.csv"; my $employee_name; my $ans; my @exact_breakdown; open(OUTFILE,">test.script"); my $ans2; if (@ARGV == 1) { chomp ($employee_name=$ARGV[0]); } else { print "\n\nPlease enter the employee's name: "; chomp ($employee_name=<STDIN>); while(1) { print "\n\nYou entered $employee_name - is this correct? <y or n +>:"; chomp($ans=<STDIN>); if ($ans =~ /[Nn]/) { print "\n\nPlease enter the server name: "; chomp ($employee_name=<STDIN>); next; } elsif ($ans =~ /[Yy]/) { last; } else { next; } } } my @name_breakdown=split(/ /,$employee_name); my @exact_check=`grep \"$employee_name\" $employee_file | cut -d : -f +2`; chomp(@exact_check); my @approx_check = `grep \"$name_breakdown[1]\" $employee_file | grep +\"$name_breakdown[0]\" | cut -d : -f 2`; chomp(@approx_check); print "EXACT MATCHES:\n"; print "=" x50,"\n"; if ($exact_check[0] ne "") { print "a. $exact_check[0]\n"; } if ($exact_check[1] ne "") { print "b. $exact_check[1]\n"; } if ($exact_check[2] ne "") { print "c. $exact_check[2]\n"; } print "\n\nAPPROXIMATE MATCHES:\n"; print "=" x50,"\n"; if ($approx_check[0] ne "") { print "d. $approx_check[0]\n"; } if ($approx_check[1] ne "") { print "e. $approx_check[1]\n"; } if ($approx_check[2] ne "") { print "f. $approx_check[2]\n"; } if ($approx_check[3] ne "") { print "g. $approx_check[3]\n"; } if ($approx_check[4] ne "") { print "h. $approx_check[4]\n"; } if ($approx_check[5] ne "") { print "i. $approx_check[5]\n"; } if ($approx_check[6] ne "") { print "j. $approx_check[6]\n"; } if ($approx_check[7] ne "") { print "k. $approx_check[7]\n"; } if ($approx_check[8] ne "") { print "l. $approx_check[8]\n"; } if ($approx_check[9] ne "") { print "m. $approx_check[9]\n"; } print "Please select which is correct: "; chomp($ans2=<STDIN>); if ($ans2 =~ /[Aa]/) { @exact_breakdown = split(/,/,$exact_check[0]); print OUTFILE "$exact_breakdown[0],TESTING,$exact_breakdown[1],$ex +act_breakdown[2]"; } elsif ($ans2 =~ /[Bb]/) { @exact_breakdown = split(/,/,$exact_check[1]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Cc]/) { @exact_breakdown = split(/,/,$exact_check[2]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Dd]/) { @exact_breakdown = split(/,/,$approx_check[0]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Ee]/) { @exact_breakdown = split(/,/,$approx_check[1]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Ff]/) { @exact_breakdown = split(/,/,$approx_check[2]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Gg]/) { @exact_breakdown = split(/,/,$approx_check[3]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Hh]/) { @exact_breakdown = split(/,/,$approx_check[4]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Ii]/) { @exact_breakdown = split(/,/,$approx_check[5]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Jj]/) { @exact_breakdown = split(/,/,$approx_check[6]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Kk]/) { @exact_breakdown = split(/,/,$approx_check[7]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Ll]/) { @exact_breakdown = split(/,/,$approx_check[8]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; } elsif ($ans2 =~ /[Mm]/) { @exact_breakdown = split(/,/,$approx_check[9]); print OUTFILE "$exact_breakdown[0],THINGS,$exact_breakdown[1],$exa +ct_breakdown[2]"; }

In reply to Better Way to Search Large File. by walkingthecow

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, details, 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, summary, 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.