Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The data in the text file is the same as before. Nothing different. The error i get is.

bad data </home/vlr/archive/temp.txt> at ./vlr-cdma-test.pl line 32, <$fh> line 1.

These are my codes. #vlr-cdma-parse.pl
#!/usr/bin/perl -w use strict; use warnings; use Text::ParseWords; my $dir = "/home/vlr/archive/"; my $date = "20211201"; chomp($date); # Check directory for relevant files opendir(DIR, $dir) or die "Could not open ".$dir."\n"; my @dir = grep(/^USRINF.*.$date.*.1700.txt$/, readdir DIR); closedir DIR; my $file = $dir."temp.txt"; open my $FH, '>', $file; foreach $_(sort @dir){ my $DFile = $dir.$_; print "Processing: ".$DFile."\n"; # Open Data File and parse each line open my $DF, '<', $DFile or die "Can't open $DFile $!"; foreach $_ (<$DF>){ chomp( $_ ); if((length $_ > 0)&&($_ =~ /^\d{15}/)){ $_ =~ s/\s+/;/g; my($imsi,$mdn,$sec) = (quotewords('[\t;]+|,\s', 0, $_))[0, +1,3]; if((($imsi =~ /^\d{15}/)&&($imsi =~ /^(702|310)/))&&(($mdn + =~ /^\d{10}/)&&($mdn =~ /^(501)/))){ print $FH $imsi.",".$mdn.",".$sec."\n"; } } } close $DF; } close $FH;
Then I would like to call the text file with your code. #vlr-cdma-test.pl
#!/usr/bin/perl -w use strict; # https://perlmonks.org/?node_id=11140267 use warnings; use Data::Dumper qw(Dumper); my $dir = "/home/vlr/archive/"; my $file = $dir."temp.txt"; open my $fh, '<', \$file or die; # FIXME change for your file my $thirty = 1; # FIXME 1 for testing, should be 30 my (%answer, %lines); $lines{ /,(.+)/ ? $1 : die "bad data <$_>" }++ while <$fh>; for ( keys %lines ) { my ($value, $key) = split ','; $lines{$_} > $thirty and push @{ $answer{$key} }, $value; } open my $FH, '>', 'output.txt'; print $FH Dumper(\%answer); close $FH;

In reply to Re^4: Parse file and creating hashes by PerlMonger79
in thread Parse file and creating hashes by PerlMonger79

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-19 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found