perl

How should I print out a particular character in the file after reading the file?

Hi, I am reading a file using perl script. This file consists of strings with different characters and I am supposed to identify strings containing the character 'X'. I want to know how should I (1) print this string (containing 'X') and also (2) write this string to another file (3) count the number of 'X' characters in the whole file....

Why does perl "hash of lists" do this?

I have a hash of lists that is not getting populated. I checked that the block at the end that adds to the hash is in fact being called on input. It should either add a singleton list if the key doesn't exist, or else push to the back of the list (referenced under the right key) if it does. I understand that the GOTO is ugly, but I've ...

removing duplicate hash value and keeping a count of it in perl

Lets say %hash = ( key1 => 'one', key2 => 'two', key3 => 'three', key4 => 'two', key5 => 'one', ); I want to have two arrays: This array should have unique key/value @array1=(key1 one key2 two key3 three) this array should have count of duplicates by value (eg here only three value are unique...

Include Perl in Java

Hi, Is there any way to execute perl code without having to use Runtime.getRuntime.exec("..."); (parse in java app)? ...

Perl: $SIG{__DIE__}, eval { } and stack trace

I have a piece of Perl code somewhat like the following (strongly simplified): There are some levels of nested subroutine calls (actually, methods), and some of the inner ones do their own exception handling: sub outer { middle() } sub middle { eval { inner() }; if ( my $x = $@ ) { # caught exception if (ref $x eq 'ARRA...

Interpolating variables in a Parse::RecDescent regex

I'm working on a Parse::RecDescent grammar to read a given human-readable set of rules and then spit out a file that is much easier for a computer to read. One of the tokens is a list of "keywords"; about 26 different keywords. These may change over time, and may be referenced by multiple pieces of code. Consequently, I want to store th...

Perl regex: replace all backslashes with double-backslashes

Within a set of large files, I need to replace all occurrences of "\" with "\\". I'd like to use Perl for this purpose. Right now, I have the following: perl -spi.bak -e '/s/\\/\\\\/gm' inputFile This command was suggested to me, but it results in no change to inputFile (except an updated timestamp). Thinking that the problem might ...

How do I get the type of a blessed ref? (ARRAY|HASH|SCALAR)

I'm passed a reference, and I want to know its type. For this purpose, "ref" works on unblessed references, but on blessed references it returns the package name it was blessed with. $a=[]; print ref $a; ARRAY bless $a, 'mytype'; print ref $a; mytype How can I get the type? ...

In Perl module Proc::ProccessTable, why does pctcpu sometimes return 'inf', 'nan', or a value greater than 100?

The Perl module Proc::ProcessTable occasionally observes that the pctcpu attribute as 'inf', 'nan', or a value greater then 100. Why does it do this? And are there any guidelines on how to deal with this kind of information? We have observed this on various platforms including Linux 2.4 running on 8 logical processors. I would guess ...

automatically get loop index in foreach loop in perl

If I have the following array in Perl: @x = qw(a b c); and I iterate over it with foreach, then $_ will refer to the current element in the array: foreach (@x) { print; } will print: abc Is there a similar way to get the index of the current element, without manually updating a counter? Something such as: foreach (@x) { ...

What is the best way to convert HTML into Excel

Hi, I have an HTML page which has a flash chart(FusionCharts) and HTML table. I need to convert this whole thing into Excel. HTML table should be displayed in cells of excel sheet. Flash chart can be displayed as an image. Is there any open source API that we could use for achieving this. Could you let me know what are the possible opt...

Multiple Page Check Box Handling

Hi, In GMail, the mails are listed. When we we have lots of mails (ex:50), we can select and go to next page, select some more mail, and come back to page 1. But whatever mails the user checked will still be checked. I just want to implement the same operation. How would I do that? Thanks, Krish Note: I don't want to use AJAX. I'...

Perl and CopSSH

I'm trying to automate a process on a remote machine using a python script. The machine is a windows machine and I've installed CopSSH on it in order to SSH into it to run commands. I'm having trouble getting perl scripts to run from the CopSSH terminal. I get a command not found error. Is there a special way that I have to have perl ins...

How to setup messages in Perl to be processed?

What is the best way for a server to process messages in Perl? I'm trying while ( 1 ) { # Get Queue Messages # Do Work sleep( 10 ); } My mysql schema is of the sort create table message ( id int auto_increment primary key, processed int, message varchar(100) ) and in my "#Get Queue Messages", I do a request like selec...

How do I pass the currently logged in user's credentials to a web service using Integrated Windows Auth from Perl

I am having a frustrating time trying to do something with Perl that would take a couple of lines of code in C#, namely to call a web service on a Windows server that requires Integrated Windows Authentication. The most likely candidate I've found for success is a module called LWP::Authen::Ntlm, but all the examples I've googled requir...

Why does my Perl script fail on "~/" but works with "$ENV{HOME}"?

I have been using this script of mine FOREVER and I have always been using "~/" to expand my home directory. I get into work today and it stopped working: #if ( $output eq "" ) { $output = "~/tmp/find_$strings[0].rslt" } # BROKEN if ( $output eq "" ) { $output = "$ENV{HOME}/tmp/find_$strings[0].rslt" } #WORKS ... open OUT_FILE,...

How can I call another script repeatedly?

Hi all, I'm trying to write a Perl script that calls another script, that reads an entire directory. I don't get any errors, but i don't get the expected result either. I don't get anything on the screen. I don't need to output anything on screen. The script I'm calling in the exec() statement should read each image file into the DIR di...

Additional hash lookup using 'exists'?

I sometimes access a hash like this: if(exists $ids{$name}){ $id = $ids{$name}; } Is that good practice? I'm a bit concerned that it contains two lookups where really one should be done. Is there a better way to check the existence and assign the value? ...

Why is IPC::Open2::open2 returning the parent process ID?

I have the following script running in Perl 5.10 in cygwin: use IPC::Open2; use Symbol qw(gensym); my $in = gensym(); my $out = gensym(); my $pid = open2($out, $in, "$exe"); waitpid $pid, 0; The value of $pid is the the PID of the perl process running, not that of the executable pointed to by $exe. Any ideas? ...

Why is the Perl script reading files from the directory in random order?

I have written a Perl script which opens a directory consisting of various files. It seems that the script does not read the files in any sequential order (neither alphabetically nor size wise) instead it reads them randomly. I was wondering what could be the reason behind the same? ...