perl

How can I find the smallest value in a Perl hash of hashes of arrays?

I want to find the easiest way of finding the hash values City { city1 -> Street1 -> [ high_street , 2] street2 -> [ low_street , 2] city2 -> Street1 -> [ high_street1 , 2] street2 -> [ low_street2 , 2] city3 -> Street1 -> [ high_street1 , 1] street2 -> [ low_street2 , 1] } This structure is sor...

How can I print the names of the files being processed in a Perl one-liner?

I generally use perl one liners instead of grep to search through files. For example following prints all the locations containing #include <stdio.h> perl -ne "print if(/#include\s*[\"<]stdio.h/)" */*.[ch] But I could not find a way to print the filename that got these lines. I tried to print $ARGV[0] but no avail. So, how do you pr...

How can I merge CSS definitions in files into inline style attributes, using Perl?

Many email clients don't like linked CSS stylesheets, or even the embedded <style> tag, but rather want the CSS to appear inline as style attributes on all your markup. BAD: <link rel=stylesheet type="text/css" href="/style.css"> BAD: <style type="text/css">...</style> WORKS: <h1 style="margin: 0">...</h1> However this inline style a...

How can I compare file list from a tar archive and directory?

I am still learning Perl. Can anyone please suggest me the Perl code to compare files from .tar.gz and a directory path. Let's say I have tar.gz backup of following directory path which I have taken few days back. a/file1 a/file2 a/file3 a/b/file4 a/b/file5 a/c/file5 a/b/d/file and so on.. Now I want to compare files and directories ...

Why do some of my SOAP::Data requests not get a response?

I am using to store data in table using SOAP::Lite and SOAP::Data. I am facing the issue where if I send 5 requests, I am only getting 2 or 3 or sometimes zero responses. What is the issue? How can I resolve this? ...

How do I compare two hashes in Perl without using Data::Compare?

How do I compare two hashes in Perl without using Data::Compare? ...

What's the difference between cmpStr and cmpStrHard in Perl's FreezeThaw?

What is the difference between cmpStr and cmpStrHard in FreezeThaw? They are mentioned in the FAQ How do I test whether two arrays or hashes are equal? ...

Why does Perl's glob return undef for every other call?

I'm not necessarily looking for a better way to do this, rather an explanations of the output would greatly be appreciated. Recently, a senior programmer asked me why his code worked but only for one instance. What I came to find out was that it worked every other occurrence. Here is my example: #!/usr/bin/perl -w use strict; my @li...

How can I use Perl's Test::Deep::cmp_deeply without increasing the test count?

It looks like Test::Deep was inspired by is_deeply. My question is how do I make cmp_deeply part of a test instead of a test on its own? Because my list of tests only states 8, but everytime I use cmp_deeply, it counts as a test, making my actual number of tests 11 (because I call cmp_deeply 3 times) when I only have 8 functions. I do n...

How can I parse a strftime formatted string in Perl?

Hi, I am new to Perl and I wan to know whether there is an inverse function to the strftime(). Look, use POSIX qw(strftime); print strftime("%YT%mT%d TTTT%H:%M:%S", localtime) I get: 2009T08T14 TTTT00:37:02. How can I do the oposite operation? From "2009T08T14 TTTT00:37:02" string to get 2009-08-14 00:37:02, knowing the formatting str...

How can I make Perl's Getopt::Long not process a literal = in a parameter name?

I wrote a Perl program "transfer.pl" and the input parameter is the hash value (the key and the value are strings). The code segment is: my %transfers = (); if (!GetOptions("transfer=s" => \%transfers)) { Usage(); exit(1); } I used the windows system. On the command line, I typed: perl tranfer.pl --transfer "table = %s"="...

How can I refactor away this repeated series of "if" blocks?

I smell something bad here? if ($col == 24) { $buffer{'Y'} = trim($val); return; } if ($col == 25) { $buffer{'Z'} = trim($val); return; } if ($col == 26) { $buffer{'AA'} = trim($val); return; } if ($col == 27) { $buffer{'AB'} = trim($val); re...

What are some one-liners that can output unique elements of the nth column to another file?

I have a file like this: 1 2 3 4 5 6 7 6 8 9 6 3 4 4 4 What are some one-liners that can output unique elements of the nth column to another file? EDIT: Here's a list of solutions people gave. Thanks guys! cat in.txt | cut -d' ' -f 3 | sort -u cut -c 1 t.txt | sort -u awk '{ print $2 }' cols.txt | uniq perl -anE 'say $F[0] unless $...

How can I get the date of an email using Perl's Mail::MboxParser::Mail?

This is a simple question. I have a little program here that reads a list of emails in a specific inbox of a user account specified by the program. I can access an account using its username, password and host. The only problem is I don't know how to get the date on each of these mails. Here's some part of my code: my $pop = new Mail::...

How can I use the shell's process substitution in Perl's system()?

Why does this line of Perl break? system("paste <\( cut -f2 $file \) $file2 > $fileout"); What's the correct way to do it? ...

Parsing files that use synonyms

If I had a text file with the following: Today (is|will be) a (great|good|nice) day. Is there a simple way I can generate a random output like: Today is a great day. Today will be a nice day. Using Perl or UNIX utils? ...

How can I use PDF 1.6 documents in Perl's CAM::PDF?

I am running into some problems using CAM::PDF with PDF documents which are %PDF-1.6 Is there a way to convert those into 1.3? (preferably a free batch-like way...) What I am currently doing is print the files using the free PDF995. The resulting PDF file is %PDF-1.3. However, it would take me forever to convert lots of documents. ...

How can I create a website summary with Perl?

When you share something on Facebook or Digg, it generates some summary of the page. How would I do this in Perl? What algorithms are there? For example: If I go to Facebook and tried to share this question as a link: http://stackoverflow.com/questions/1279851/facebook-digg-get-website-summary It retrieves "Facebook/Digg get website...

How do I fix "Symbol not found: _is_prefix" when compiling Perl's DBD::mysql?

First I wanted to build the DBD::mysql package. That kept failing because whatever make resulted in could not be loaded for the tests with a Symbol not found: _is_prefix. So I assumed that cpan might be a tad old. I know it's a random assumption, but cpan did tell me to install the latest Bundle::CPAN. Who's successfully installed eithe...

Why can't my Perl object find its skip() method, even though I can call it as a subroutine?

I'm working on a Perl module and whenever I call the skip() method I wrote in the following way: $cursor->skip(4); I get: Undefined subroutine Perl finds it! Oddly, if I name "skip" anything else ("skipper", "hello"), this syntax works: $cursor->skipper(4); I thought maybe skip() was a "secret" reserved key word or somethi...