CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Reading a file from reverse and Storing the data

 



Tejas
User

Jul 11, 2016, 12:49 PM

Post #1 of 2 (1484 views)
Reading a file from reverse and Storing the data Can't Post

This CODE reads the file from top to bottom as usual
my requirmnet is to read it reverse and store the data undr the regex headings below.




Code
my $CURRENT_REPORT_PART1_REGEX    = 'Start of CURRENT Report Part 1 Summary'; 
my $END_SEARCHING_REGEX = 'Retired Alert IDs Since Last Interval';

sub MyFunc
{
my $expAddress = 780200700103;
my @fileData = <DATA>;
foreach my $input_line (@fileData)
{
if ($input_line =~ m/$CURRENT_REPORT_PART1_REGEX/)
{
$TableName = $CURRENT_REPORT_PART1;
next;
}
elsif ($input_line =~ m/$END_SEARCHING_REGEX/)
{
$currentTableName = "";
$filteredTableEntriesMap{$REPORT_PART1} = [@ReportP1DataList];
return (true, \%filteredTableEntriesMap);
}

if ($TableName eq $CURRENT_REPORT_PART1)
{
if (does_ReportP1_dataEntry_contains($input_line, $expAddress))
{
push (@ReportP1DataList, $input_line);
}
}
}
outputLog ("STEP FAILED: report file is empty or no expected data is found");
return (false, \%filteredTableEntriesMap);
}


sub does_ReportP1_dataEntry_contains
{
my ($line, $CURRENTNo) = @_;
return ($line =~ m/^\s*$\s+/);
}

__DATA__
Retired Alert IDs Since Last Interval Summary at CST Time: Wed Oct 14 14:32:34 2015
-------------------------------------------------------------------------------------
----NO ALERTS RETIRED----
-------------------------------------------------------------------------------------

Start of Current Report Part 1 Summary
-----------------------------------------------------------------------------------------------------------------------------------------------

Current Current Mode Link State Comm Lvl

-----------------------------------------------------------------------------------------------------------------------------------------------
780200700103 ON DEMAND MONITORING 99.36
-----------------------------------------------------------------------------------------------------------------------------------------------

Start of Current Report Part 2 Alert Summary
------------------------------------------------------------------------------------------------------------------------------

Current Current Mode Link State

------------------------------------------------------------------------------------------------------------------------------
----No Currents Have Active Alerts----
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Active Current Alert Summary at UTC Time: 1444852174 CST Time: Wed Oct 14 14:32:34 2015 to Wed Oct 14 14:49:34 2015
--------------------------------------------------------------------------------------------------------------------------------------
Current Comm Alert ID Delay Alert ID Seq Alert ID Overall Alert ID Freq Alert ID WSBE Exec Alert ID Cell Tx Alert ID
--------------------------------------------------------------------------------------------------------------------------------------
----No Active Alerts For Any Current----
-----------------------------------------------------------------------------------------------

Start of Current/Base Station Link Report Summary Part 1 at UTC Time: 1444852174 CST Time: Wed Oct 14 14:32:34 2015 to Wed Oct 14 14:49:34 2015
------------------------------------------------------------------------------------------------------------------------------------------------

Current Base Station Current Mode Link State Comm Lvl

------------------------------------------------------------------------------------------------------------------------------------------------
780200700103 up.v.000315 ON DEMAND MONITORING 99.36

Start of Current/Base Station Link Duplicate Report Summary at UTC Time: 1444852174 CST Time: Wed Oct 14 14:32:34 2015 to Wed Oct 14 14:49:34 2015
----------------------------------------------------------------------------------------------
Current Base Station Current Mode Link State Duplicate Message Count
----------------------------------------------------------------------------------------------
----No Links with Duplicates----
------------------------------------------------------------------------------------------------------------------------------------------------

Start of Current/Base Station Link Report
------------------------------------------------------------------------------------------------------------------

Current Base Station

------------------------------------------------------------------------------------------------------------------
----No Links Have Active Alerts----
------------------------------------------------------------------------------------------------------------------

Active Link (Current/Base Station) Alert Summary
-------------------------------------------------------------------------------------------------------------------------------------------
Current Base Station
-------------------------------------------------------------------------------------------------------------------------------------------
----No Active Alerts For Any Link----
-------------------------------------------------------------------------------------------------------------------------------------------

Active Alert ID Summary at CST Time: Wed Oct 14 14:49:34 2015
-------------------------------------------------------------------------------------
----NO ACTIVE ALERTS----
-------------------------------------------------------------------------------------

Retired Alert IDs Since Last Interval Summary at CST Time: Wed Oct 14 14:49:34 2015
-------------------------------------------------------------------------------------
1 2 3
-------------------------------------------------------------------------------------




The above code stors the info when file reads top to bottom
I would want to read th file in reverse way and store the values below that heading in a data structure as shown above.

I would want to read the file from last to first and stop when the regex matches END_SEARCHING_REGEX

Ultimately, The output should be whethr $expAddress is available in the table or not.

Any Suggstions,



Thanks
Tejas


(This post was edited by Tejas on Jul 11, 2016, 12:56 PM)


Laurent_R
Veteran / Moderator

Jul 12, 2016, 12:55 AM

Post #2 of 2 (1466 views)
Re: [Tejas] Reading a file from reverse and Storing the data [In reply to] Can't Post

Hi,

if you want to read your file backward line by line, then PerlIO::reverse (http://search.cpan.org/~gfuji/PerlIO-Util-0.72/lib/PerlIO/reverse.pm) probably does what you need.

An alternative might be File::ReadBackwards (http://search.cpan.org/~uri/File-ReadBackwards-1.05/ReadBackwards.pm. It also does a line-by-line backward file reading, and this is presumably what you want.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives