Hello, I am just getting started with Perl and I have not used any programming languages in the past. I hope you can help me with this.
I am not able to effectively use hashes to parse, sort or replace data from a text file. I have read through a couple of books (Learning Perl, Perl by Example) and browsed Perl websites, but I am still having difficulty grasping the concepts. For example, I am trying to take a text file and sort the results in any way I choose. I can't seem to find a real-world example where I read a file into a hash and parse/sort and/or replace data. Here is my code for parsing data using arrays which works:
#!/usr/bin/perl
use strict;
use warnings;
open (FILE, '<', 'FB_LPWAP.txt') or die ("ERROR: Could not read file"
+);
print <FILE>; #Prints entire file
while (<FILE>){
my @array = split /\t/, $_;
print "$array[0]\t"; #Date
print "$array[1]\t"; #Closing Price
print "$array[2]\n"; #Weighted Average Price
}
close (FILE);
I still have a lot to learn, but I am hoping someone could help point me in the right direction as I have not be able to use hashes on a text file that has multiple columns of data without receiving a number of syntax/compilation errors. The text file I have has multiple columns and I would like to parse certain columns using hashes. I also have would like to sort the data. I don't know how to reference my columns. Here is the sample data in my test file:
TICKER| CO. NAME| PRICE| MARKET CAP| INDUSTRY
ABC | ABC Co.| 15.5| 5000| Industrials
AB | Alpha Beta| 12| 2500| Materials
DOZ | ZZZZZ| 5.05| 2800| Telecom
DX | DX Co.| 77.2| 12000| Industrials
DXX | DXX Co.| 50.25| 9000| Utilities
Thank you.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.