Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: How to read zipped file in perl

by Eily (Parson)
on Jan 09, 2014 at 09:43 UTC ( #1069935=note: print w/replies, xml ) Need Help??

in reply to How to read zipped file in perl

This works far better than it should, (split /\|/)[4] means that you read values separated by a |, and take the fifth (Edit: not fourth, thanks choroba). Since your values are separated by commas, that's either not your real input or the code you actually used. Do read the documentation on split if you intend to use it. The [4] means "only the fifth value", and join is used to join several values, so in this case, it is useless.

For your expected result, you could do something like that :

while (my $line = <$handle>) { my ($number, $key1, $dontcare, $country, $key2) = split /:/, $line; + # Something to change here $result{$key1}{$key2}++; } print Dumper(\%result);
You still have to change something for the split to work, that's because I didn't want you to just copy and paste my code without understanding anything, and because I'm not really sure what you values separator actually is. In the list of variable in the my(), you should rename key1 and key2 to give them meaningful names, and you can replace all the variables you won't use by undef.

To have something more useful with Data::Dumper, you should write print Dumper(\%result);, the backslash returns a reference to your hash, which means that instead of using Dumper on each element (key or value) of %result, you'll do it on %result as a whole.

There is one thing you do correctly though, and it's reading from a zipped file. So you don't have to ask for that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1069935]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2017-12-11 12:36 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (290 votes). Check out past polls.