Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: Parse file and creating hashes

by PerlMonger79 (Sexton)
on Jan 08, 2022 at 23:49 UTC ( [id://11140285]=note: print w/replies, xml ) Need Help??


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

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;

Replies are listed 'Best First'.
Re^5: Parse file and creating hashes
by tybalt89 (Monsignor) on Jan 09, 2022 at 00:33 UTC
    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.

Re^5: Parse file and creating hashes
by tybalt89 (Monsignor) on Jan 08, 2022 at 23:53 UTC

    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-26 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found