Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Parse file and creating hashes

by tybalt89 (Monsignor)
on Jan 08, 2022 at 21:07 UTC ( [id://11140279]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Parse file and creating hashes
in thread Parse file and creating hashes

Here's another way...

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11140267 use warnings; my $file = <<END; 702005010683593,5016683593,7020000024140 702005010640383,5016640383,7020000024150 310005010532143,5016532143,7020000034001 702005010637702,5016637702,7020000034001 702005010608274,5016608274,7020000034013 702005010608274,5016608274,7020000034013 310005010609604,5016609604,7020000034013 702005010510869,5016510869,7020000034013 702005010551513,5016551513,7020000034130 702005010551513,5016551513,7020000034130 702005010679719,5016679719,7020000034222 702005010527052,5016527052,7020000034222 702005010645458,5016645458,7020000034222 END 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; } use Data::Dump 'dd'; dd \%answer;
Server Error (Error ID 2654017c2132783)

An error has occurred. The site administrators have been notified of the problem and will likely soon fix it. We thank you, for you're patients.

Replies are listed 'Best First'.
Re^4: Parse file and creating hashes
by PerlMonger79 (Sexton) on Jan 08, 2022 at 22:13 UTC
    Hello, Thanks again for your help, I really appreciate it. I hate to bother ask, but is there a way to output without the use of Data::Dump? The issue is that the server where the script is hosted is on a private lan network which does not have access to the internet, I need to vpn in to work on it remotely, so I cannot install the module on the server. That's why I had the module use Excel::Writer::XLSX; because I was going to output to excel file as a report. I couldn't test the script. :(
Re^4: Parse file and creating hashes
by PerlMonger79 (Sexton) on Jan 08, 2022 at 22:52 UTC
    Thank you very much. I really appreciate all your help. With this part of the script done, I can continue with the other parts. Thanks again and God bless. :)
Re^4: Parse file and creating hashes
by PerlMonger79 (Sexton) on Jan 09, 2022 at 00:36 UTC
    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;

      Yep, Remove the backslash before $file in the open statement.

Re^4: Parse file and creating hashes
by PerlMonger79 (Sexton) on Jan 08, 2022 at 23:49 UTC
    Hi again. When I run your script it works fine, but when I try to call a text file from the directory where the data is previously stored I get the "bad data" error. Is there something I changed which is causing this error? How can I use the data from a called text file?
    #!/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;
      open my $fh, '<', $file or die; # FIXME change for your file

      Remove the backslash, it is only used when you are treating a string as a file.

      Are the lines in the data file in a different format than the lines you showed as an example?

      Also, please show the complete line with the "bad data" error message, it will also show the line it thinks is bad.

Re^4: Parse file and creating hashes
by PerlMonger79 (Sexton) on Jan 09, 2022 at 01:09 UTC
    Hi, I don't think it is reading the text file. This was the output.

    $VAR1 = {};

    Thanks for taking the time to help.

      That would happen if there were no duplicates in your file.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-03-29 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found