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: Regular Expressions:
Need help in matching

 



aaron
New User

Nov 17, 2010, 10:36 AM

Post #1 of 2 (2974 views)
Need help in matching Can't Post

I need help in matching a text file and store its content in the following way

Content is

JAN|
graph|104|
success|100|
on_time|100|
environment|1|
builder|3|
design|0|
FEB|
graph|95|
success|100|
on_time|100|
environment|1|
builder|3|
design|0|
.
.
.
.
goes till DEC

all i want is i would have to check if its JAN, FEB or MAR and the copy that in a month array and then i will have a input argument say "graph" so it will match with the graph keyword and pick all the graph values of all the month and put it in a separate array say @data_arr.

Any help on this would be greatly appreciated...

Thanks


shawnhcorey
Enthusiast


Nov 17, 2010, 4:49 PM

Post #2 of 2 (2949 views)
Re: [aaron] Need help in matching [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

my $Month = '';
my %Data = ();

while( <DATA> ){
chomp;
my @fields = split /\|/;
if( defined( $fields[1] ) && length( $fields[1] ) ){
$Data{$Month}{$fields[0]} = $fields[1];
}else{
$Month = $fields[0];
}
}

print Dumper \%Data;

__DATA__
JAN|
graph|104|
success|100|
on_time|100|
environment|1|
builder|3|
design|0|
FEB|
graph|95|
success|100|
on_time|100|
environment|1|
builder|3|
design|0|


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


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

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