Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Issue in parsing data from file

by sachin raj aryan (Acolyte)
on Aug 08, 2019 at 04:43 UTC ( #11104136=perlquestion: print w/replies, xml ) Need Help??

sachin raj aryan has asked for the wisdom of the Perl Monks concerning the following question:

hello monks !!! below is my text file which I am reading

1111111111 GENERAL BT 00318 06/08/19 CSH DE +P 1 +12.00 3336328 06/08/19 CSH DE +P 2 +24.00 3336328 06/08/19 CSH DE +P 1,2 +00.00 3336328 06/08/19 CSH DE +P 2,4 +00.00 3336328 2222222222 GREEN VALLY 00318 06/08/19 CSH DE +P 59.00 + 3336328 4226402 1111111111 GENERAL BT 00318 06/08/19 CSH DE +P 1 +12.00 3336328 06/08/19 CSH DE +P 2 +24.00 3336328 06/08/19 CSH DE +P 1,2 +00.00 3336328 06/08/19 CSH DE +P 2,4 +00.00 3336328 adadadadadadadasdsdasdasdasdadasdasdasddas 3333333333 GENERAL BT 00318 06/08/19 CSH DE +P 1 +12.00 3336328 06/08/19 CSH DE +P 2 +24.00 3336328 06/08/19 CSH DE +P 1,2 +00.00 3336328 06/08/19 CSH DE +P 2,4 +00.00 3336328 defghghghghghghghgghgh daadad adasdas asdasds asdasdasdasddasddad

I am reading line by line with matching pattern and not able to add the amount in same matched pattern array till the last csh dep so that I can move that data TO databse in specified columns. here is my code below but I am not able to achieve what I want . I am new to perl and not able to figure out how to achieve that<\p>

#!/usr/bin/perl use Net::FTP; use IO::Uncompress::Gunzip qw(gunzip $GunzipError); use Date::Simple qw(d8); use DBI; use DBIx::Dump; use Time::Piece; use Time::Seconds 'ONE_DAY'; if (open(my $fh,'< CASH_REPORT-cfpd0903.txt')){ #or die "Could not open file $!"; while (my $row1 = <$fh>) { if ($row1=~/^.*\b(CSH DEP)\b.*$/) { $row1=~s/^\s+//; my $row2 = $row1; #print "$row1 sachin is here \n"; my @sachin = split/\s+/,$row2; print "@sachin printing array\n"; } }

Replies are listed 'Best First'.
Re: Issue in parsing data from file
by poj (Abbot) on Aug 08, 2019 at 07:01 UTC

    Consider using unpack to split the lines into fixed width columns

    #!/usr/bin/perl use strict; use warnings; my $infile = 'CASH_REPORT-cfpd0903.txt'; # columns # 1111111111 # GENERAL BT # 00318 # 06/08/19 # CSH DEP open my $fh,'<', $infile or die "Could not open file '$infile' : $!"; while (my $row = <$fh>){ my @col = unpack "a14 a27 a11 a11 a9",$row; print join '|',@col,"\n" if $col[4] =~ /CSH DEP/; }
    poj
Re: Issue in parsing data from file
by roboticus (Chancellor) on Aug 08, 2019 at 13:19 UTC

    sachin:

    When you have a structured file containing master and detail records, you typically collect all your data until you reach the next master record. At that point, you can process all the data (master and detail) for the previous master record. Something like:

    my $account; my @transactions; while (my $row = <$fh>) { my @fields = unpack "a14 a27 .....", $row; if ($fields[0] ne "") { # New record has account number, so handle the info we've gath +ered so far. process_details($account, [ @transactions ]); # Now start collecting information about this new account $account = $fields[0]; @transactions = (); } # Add transaction details to list push @transactions, get_transaction(@fields); } # Finally, process the last transaction process_details($account, [ @transactions ]); # Handle an account with one or more transactions... sub process_details { my ($account, $aTransactions) = @_; # The first time we're called, we won't actually *have* any # information, so do nothing return unless defined $account; # Do what needs to be done... } # Fetch transaction details from fields sub get_transaction { my @fields = @_ # Fetch what you need from the fields and create a transaction return $transaction; }

    This is "control break" processing. You can extend it to have multiple levels if needed.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Issue in parsing data from file
by haukex (Bishop) on Aug 08, 2019 at 05:34 UTC

    You have reposted several duplicates of this question, please be careful and don't submit the same question multiple times - if you have the submission page open in your browser, please don't reload it (your browser should normally warn you about resubmitting).

      if you have the submission page open in your browser, please don't reload it (your browser should normally warn you about resubmitting).

      And perlmonks should really be smarter about form submissions. Simply generating a redirect from POST to GET would completely elimitate the problem, at the cost of an additional HTTP request. See also Re^3: /perl5.10/bin and linux.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        afoken :) old news outside of pmdiscuss goes unnoticed

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-01-16 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (49 votes). Check out past polls.

    Notices?