perl,hash , Perl reading from file into hash

Perl reading from file into hash


Tag: perl,hash

I would like to read the contents of a file into a hash structure. The header of the file should be the key in the hash structure, as well as the values being the content listed below each column.

The file that I am going to read from will look something like this.

Recv-Q Send-Q             Local Address:Port               Peer Address:Port 
0      128                            *:111                           *:*      users:(("rpcbind",1268,8))
0      128                            *:53845                         *:*      users:(("rpc.statd",1404,9))
0      128                            *:22                            *:*      users:(("sshd",1577,3))
0      128                                     *:*      users:(("cupsd",1452,7))
0      100                                      *:*      users:(("master",1686,12))

I would like to have say Port as a key, containing the keys: 111, 53845, 22, 631, 25. How would I go about in order to read this file into the hash in the structure that I outlined?


I'll offer you a basic starter for 10 on how to parse your data.

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper; 
my @header = split ( " ", <DATA> );

my %service_on;

while ( <DATA> ) {
    my ( $recvq, $sendq, $locaddr_port, $peeraddr_port, $thing ) = split;
    my ( $locaddr, $port ) = split ( ":", $locaddr_port ); 
    $service_on{$port} = $thing; 

print Dumper \%service_on; 

Recv-Q Send-Q             Local Address:Port               Peer Address:Port 
0      128                            *:111                           *:*      users:(("rpcbind",1268,8))
0      128                            *:53845                         *:*      users:(("rpc.statd",1404,9))
0      128                            *:22                            *:*      users:(("sshd",1577,3))
0      128                                     *:*      users:(("cupsd",1452,7))
0      100                                      *:*      users:(("master",1686,12))

You can do a 'named field' approach if you wish, but if your data format is consistent, it doesn't matter too much. Bear in mind that your data doesn't actually look like it's tab delimited - if it isn't, splitting on whitespace is easier... but it's also going to 'break' on Peer Addr.

If you want to go down that road, you need to look at slices of a hash. However, that isn't going to work as you have a field without a header row, which might just be discarded if you do.


while ( <DATA> ) {
    my %fields;
    @fields{@header} = split; 
    my ( $locaddr, $port ) = split ( ":", $fields{$port_field} ); 
    print Dumper \%fields;


Taking multiple header (rows matching condition) and convert into a column

Hello I have a file that has multiple Headers in it that I need to have turned into column values. The file looks like this: Day1 1,Smith,London 2,Bruce,Seattle 5,Will,Dallas Day2 1,Mike,Frisco 4,James,LA I would like the file to end up looking like this: Day1,1,Smith,London Day1,2,Bruce,Seattle Day1,5,Will,Dallas Day2,1,Mike,Frisco Day2,4,James,LA The file...

Cleaner way of mapping a hash in ruby

Let's assume I need to do a trivial task on every element of a Hash, e.g. increment its value by 1, or change value into an array containing that value. I've been doing it like this{ |k, v| [k, v+1] }.to_h v+1 is just an example, it can be...

How to store database records into hash ruby on rails

I'm using mysql database. Now I want to retrieve data from my database: value and time, for plotting graph. my db store up to 4000 data,and I need plot 1000 of them. First method come to my mind is: Records.all.each do |record| points[record.time.to_s]=record.value.to_s end then cut the first 1000...

What is Fragment URLs and why to use it

I am new in PHP Development. Today I came across the interesting topic URL fragmentation specifically the '#' part of the URLs. I searched about that It says it's like\foo.html#bar. But I don't understand why this "#bar" is needed. and how to read it by PHP?...

-M Script start time minus file modification time, in days

I would expect -M $_ to be negative, but it is zero for: perl -E 'qx(touch $_), sleep(5), say -M for "/tmp/file"' Does perldoc mentions such behavior?...

Perl - an array content

Can you explain me how to check if an element belongs to array? My script needs to know whether the element has wanted extension to make a shortcut and copy it to another directory. Here is an example: my @array = qw(avi mp4 mov); my $dir = "E:\Downloads"; opendir (my...

Formating issue with md5deep

So I am making a program in batch and it requires a text file to be hashed then the hash has to be saved to a text file. I already have that part done but when it saves to the text file it saves like this f558e01b798b0390ab6206679a6926a7 C:\Users\computer\Desktop\tmpfile.txt So my...

Perl would I use fc over uc?

When would you ever need to use fc(), when would uc() ever fail? Perl fc documentation...

Opening multiple files in perl array

I have a perl script where by I assigned all the files with a .log extension to an array called @allfiles. How do I run my script for the files stored in each array? My idea is something like open(my $fn, '<', @allfiles) or die "Could not open file '@files':...

Perl Debugging Using Flags

So my goal is to find an easy way to turn on print statements in Perl a flag. In C/C++ you can use a #define to choose if certain code is run and it is a way to turn on and off debug print statements. Where if a #define DEBUG...

Get ISO DateTime with only core modules in Perl?

I would like to get a date-time string such as 2015-06-17 10:20:34 with only core modules. The reason of this is that cpan install DateTime takes ages on my machine because of the tests and the fetch of all the dependencies. If I give my Program to my colleagues. They...

Find numbers in a file and change their value with perl

I have a file with some data in it but there are a bunch of annoying numbers that are less than one which I wanted to just change to 1 instead of manually doing it. I was wondering how you would do this in perl. I tried using something like...

What does this horribly ugly (yet somehow secretly beautiful) Perl code do?

I found this code in a Powerpoint presentation about Perl scripting and this was on a page demonstrating how ugly Perl code can get if you really wanted to make it that way. The presentation says nothing about what this code does. Honestly I am just very curious to know......

Create unicode character with pack

I am trying to understand how Perl handles unicode. use feature qw(say); use strict; use warnings; use Encode qw(encode); say unpack "H*", pack("U", 0xff); say unpack "H*", encode( 'UTF-8', chr 0xff ); Output: ff c3bf Why do I get ff and not c3bf when using pack ?...

Regex in Perl Uninitialized $1

My string looks like this: <File `../Path/To/My_File.gif'> I want to extract just "Path/To/My_File.gif". Here is the check I have: if ($row =~ /(?<=File `..\/).*(?=')/) { print "Found it!\n"; print "$1\n"; } I see "Found it!" printed to the console but also get an error saying that $1 is uninitialized. What...

Capture tee's argument inside piped Perl execution

How to capture piped command's argument ? I use : perl -some_args | tee arg_filename How to get arg_filename 's value inside ? CONTEXT I need to send this filename in a mail which sends at the end. I need to use tee because we dump huge...

Ruby hash with key as hash and value as array

I am creating a hash, whose key is a hash and the value is an array. E.g., shop = items.each do |item| grouping_key = { 'name'=> item['name'], 'value'=> item['value'] } shop [grouping_key] ||= shop [grouping_key] << item end Here, I am grouping each item based on grouping key....

Why this exclusion not working for long sentences?

Command perl -ne 'print unless /.[240,]/' input.txt > output.txt which includes some sentences which are longer than 240 letters. Why? Example data Development of World Funny Society program on young people who are working hard for the sport and social life such that they have time to go pizzeria every...

How to extract some text from an HTML doc using Web::Query

I'm trying to extract the subject (between the h3 tags) in the following example using Web::Query. Find 'h3' returns the author text, but I want the h3 in the subject class instead. I tried .subject.div.h3 but it returns undef. #!/usr/bin/perl use strict; use warnings; use Web::Query; # libweb-query-perl use Data::Dumper;...

Convert md5 in base64 in md5 of 32 characters with PHP

I have an example: Md5 base64: wPE2JkrsTJxF+KbSDApwYQ== Md5 using md5_file: c0f136264aec4c9c45f8a6d20c0a7061 how convert the firts md5 in the second md5?...

Creating a sequence of unique random digits

I have the following code use strict; use warnings; use 5.22.0; # Generating random seed using # Programming Perl p. 955 srand( time() ^ ($$ + ($$ << 15 ) ) ); # Generating code that could have duplicates my @code = ( (int(rand(9)) + 1), (int(rand(9)) + 1), (int(rand(9))...

Can't locate module(s) using Mojo::DOM

I'm new to Mojolicious. I am sure this is probably a setup problem, but it's eaten up an entire day of my time. I'm trying to run this simple test code #!/usr/bin/perl use strict; use warnings; use Mojo::DOM; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new(); $ua->get('')->res->dom('a div')->ancestors('div.spacer')->each( sub { say $_->all_text...

How to copy matches from an extremely large file if it contains no newlines?

The problem is I cannot avoid working with extremely big files which contain no newlines in them: <a>text1</a>...gigabytes of data here, all in one single line...[a text to extract b> What should I do if I want to copy matches from this file (putting every match in a separate line,...

unable to understand qr interpolation

I was reading Programming Perl where I learned qr interpolation of strings as regex as : $re = qr/my.STRING/is; print $re; # prints (?si-xm:my.STRING) and it says The /s and /i modifiers were enabled in the pattern because they were supplied to qr//. The /x and /m, however, are disabled...

problems copying shared hash in perl threads

I encountered what I feel is strange behavior of shared hash in perl and needed some help understanding it. The actual problem is in a far larger code-base and I have tried reducing it to smaller reproducible script. So essentially the problem I'm facing is I have a shared variable...

Hashing speed - cryptic results (Hashing twice much slower than hashing once)

I'm trying to understand why hashing of multiple hashes is much slower than one hash. In the following test I hash a file twice - first with SHA1 and then with both SHA1 and SHA256. The first execution shows the expected results - disk read dominates the time used -...

Counting occurrences of a word in a string in Perl

I am trying to find out the number of occurrences of "The/the". Below is the code I tried" print ("Enter the String.\n"); $inputline = <STDIN>; chop($inputline); $regex="\[Tt\]he"; if($inputline ne "") { @splitarr= split(/$regex/,$inputline); } [email protected]; print $scalar; The string is : Hello the how are you the wanna work on...

What's going on with this hash? [duplicate]

This question already has an answer here: Strange ruby behavior when using Hash default value, e.g.[]) 4 answers I make a new hash with a default value of an empty array. h =[]) I push a value into the hash where the key is 'a'. h['a'].push(1243) h...

How to pass a hash as optional argument to -M in command line

I know that when we need to pass some arguments to the use keyword after a package name we can pass them in the command line after the -M parameter. For example: use feature 'say'; say 'hello!'; can be invoked from the command line with >perl -Mfeature=say -e"say 'hello!'" But...

Plain text emails displayed as attachment on some email clients

The email can be viewed normally using some email clients (Evolution, Thunderbird), but with other clients (e.g., GMX) the body of the message remains empty and an attachment containing the body of the message is sent. I would like to know how I can prevent this from happening, since the...

Perl: Multiply loops, 1 hash and regex

I got stuck with logic behind loops (while & foreach) and AoH. I have basic knowledge about loops and arrays of hashes, but I can't quite understand how to combine them into 1 single and simple solution. My task is to check regular user's password age, if it is older...

Error Hashing + Salt password

Someone can help me to fix this problem: TypeError: can't concat bytes to str I am trying to safely store hash+salt passwords, I think the problem is that my salt is a byte object how can I transform it into a string? Or is there a way to hash it...

Check for decimal point and add it at the end if its not there using awk/perl

I have test.dat file with values given below: 20150202,abc,,,,3625.300000,,,,,-5,,,,,,,,,,,,,,,,,,,,,, 20150202,def,,,,32.585,,,,,0,,,,,,,,,,,,,,,,,,,,,, 20150202,xyz,,,,12,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, My expected output is shown below: 20150202,abc,,,,3625.300000,,,,,-5.,,,,,,,,,,,,,,,,,,,,,, ^. added here 20150202,def,,,,32.585,,,,,0.,,,,,,,,,,,,,,,,,,,,,, ^. added here 20150202,xyz,,,,12.,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, ^. added here So if column 6 and 11 doesn't have decimal point in it, then we should add '.' at the end of...

Deleting upto a line

I have a line that looks like: foo cat dog = -48.34277635 foo(horse->0) = -60.34277635 and I only want the last set of numbers: -60.34277635 The line is formatted with that exact spacing. I've looked everywhere for a simpler solution, but I can't find anything without chopping the file piece...

Reading from DATA file handle

My perl module needs to use a look up table that's about 309,000 lines long. Currently the part that loads the table into an array looks (roughly) like this: use strict; use warnings; # load all the data from below my @ref_data; while (<DATA>) { push @ref_data, $_ } close...

Looping variables

I'm working with perl to make a script that will work with Dot products/assorted vector math. I've got a working script ( Still very much in progress/needs refinement ) that will do what I ask. #!/usr/bin/perl use strict; use warnings; use diagnostics; use Math::Vector::Real; use 5.010; use Math::Trig; my $source...

Version-dependent fallback code

I have a script that needs to run on multiple servers, however, each server may not have the same version of Perl available and may have differing features. Perl v5.14 introduced the /r modifier for regular expressions which returns the result of a substitution and leaves the original text alone....

choice between map or unordered_map for keys consisting of calculated double values.

In order to quickly find coplanar entities in a bunch of planar entities in 3d space, I want to create a mapping from 3d planes to the set of entities lying in that plane (estimated max around ~1000 planes and ~100000 entities) I can create my own custom class to...

Get array of values from array of hashes

I have a array like this arr = [{:id=>1}, {:id=>2}, {:id=>3}, {:id=>4}, {:id=>5}, {:id=>6}, {:id=>7}, {:id=>8}] The result should be result = [1, 2, 3, 4, 5, 6, 7, 8] I used { |i| i.values }.However, the result is [[1], [2], [3], [4], [5], [6], [7], [8]]. Please help....

How to match and remove the content preceding it from a file in unix [closed]

I have a mysql dump file, and i want to remove the content of the file after "-- Final view structure for view view_oss_user" using sed/perl. The input file is something like this : Content : rom `target` */; /*!50001 SET character_set_client = @saved_cs_client */; /*!50001 SET character_set_results = @saved_cs_results...

Why Filter::Indent::HereDoc complain when blank line in middle of HereDoc

I am trying Filter::Indent::HereDoc which allows one to indent the HereDocument. This is very useful, to be able to have HereDoc that flows with the code logic. From the above link When a 'here document' is used, the document text and the termination string must be flush with the left...

Windows/Linux child process STDIN differences

I built a simple text processing script at work to be used by another program. When I was done, someone remembered that the script needs to not block STDIN/STDOUT for the tool using it to work right, and modified the script accordingly. The script opens *nix's cat in a subprocess...

Command line arguments in Perl

I am working on an open source project for GSoC and I have this piece of Perl code with me. I need to create another Perl file for a similar task. However, I am having trouble understanding 3 lines of this file. More specifically, I am not able to understand...

Perl: Using Text::CSV to print AoH

I have an array of hashes (AoH) which looks like this: $VAR1 = [ { 'Unit' => 'M', 'Size' => '321', 'User' => 'test' } { 'Unit' => 'M' 'Size' => '0.24' 'User' => 'test1' } ... ]; How do I write my AoH to a CSV file with separators,...

IDE doesn't recognize the method

I'm trying to acces the cityMethod() inside the class City. class City { void cityMethod() { } } So, I do: map<string,City> mymap; City c; mymap["Madrid"] = c; Now, when I do this: mymap["Madrid"].cityMethod(); Ok, it works. But the IDE(Qt) doesn't recognize the "cityMethod". Am I doing something wrong? Is...

Perl : Display perl variable awk sed echo

When I am using below command directly its working fine but when I am trying to put this in perl script its giving lots of error. my $calculate = `echo "$value" | awk -F "SP=" '{print $2}' | awk -F ";" '{print $1}' | awk -F ":" '{print $2}' |...

Hashing Password in ASP.NET and Resolving it In SQL Procedure,,stored-procedures,hash,password-protection
I'm developing an ASP.NET application using SQL Server Stored Procedures. I need to hash my login password and resolve it in my sp_LoginCheck Stored Procedure. Any suggestions? I have already inserted data in the database. For example: UserName/Password ABC/123456 DEF/987654 I want to encrypt or hash whatsoever the password then...

calling cgi script from other cgi script

I found a puzzling behavior using perl cgi for which -- I guess -- there is a perfectly valid explanation but I couldn't find one. There is this question on stackoverflow, but what it describes seems to fail for me. The situation: I have two perl cgi scripts. One of...