Perl: remove element from a list/array

You can easily push an element to a list/array, but when it comes to deleting an element, unless you know exactly its index number (which is rarely a case, as far as I’m concerned), things get unreasonably complicated.

I googled around and talked to people on IRC here’s what seems to be the best approaches:

  1. use splice in a loop with a counter
  2. use List::UtilsBy qw ( extract_by );

splice(); proved to be a really hard case. The following code will only remove 1st occurence in an array and ignore the rest, due to the fact that splice causes elements in a list to update their index numbers, so they get essentially offset and the counter is never adjusted for this fact (because I’m too lazy to figure out how). This code will do the job,  if your list contains unique elements  only, though.

#!/usr/bin/perl

use strict;
use warnings;

my @arr = (1, 2, 3, 4, “abc”, “zxy”, “qwerty”, “abcdef”);

my $index = 0;

foreach (@arr) {
if ($_ =~ m/abc.*/i) {
splice (@arr, $index, 1)
} else { $index++ }
}

If you print @arr to stdout you’ll see that abcdef is still there.

By and large, it’s a bad idea to modify an array in a for loop. If you one of a pretty, intuitive way to write a code that removes elements from an array do let me know, please.

extract_by looks like a real deal, but I abandoned it before I started really using it because I’ve realized that my code didn’t actually need to delete any elements from a list at all lol

Advertisements

Placing Evolution windows to specific viewports with Compiz

Compiz has lots of nifty plug-ins and one of them is called Place. What it does essentially it takes a window and puts it on a specific desktop you tell it to.

The tricky part is to write rules to select windows. Evolution proved to be quite a pain to handle. I usually use 6 virtual desktops, or viewports in Compiz parlance. I have two rows of them, each has 3 virtual desktops. I’m used to having an e-mail client running in viewport 3, row 1 (X3:Y1) web-browser and console in viewport 1 (X1:Y1).

Well, initially my idea was to  grab a Window Title for Evolution’s Compose Message window and have it explicitly excluded in the rules. That kind of approach worked just fine on my office workstation where I use Thunderbird, but with Evolution this sort of rule:

title=Evolution & !title=”Compose Message”

Just didn’t work. What happened was either Compose Message window would be still forcibly placed to viewport 3, or, totally weird, main Evolution window would be transferred to active viewport and Compose Message window would take place of main Evolutoin window defined by Place plugin rules (in my case viewport 3, X3:Y1).

But as I said a little earlier, I keep my Chromium window in viewport 1 and when I click an “e-mail to” link I’d love to see Compose Message window in viewport 1, or in other words I want it to follow the viewport I’m working in. Well, that logic expressed in a rule above proved to be a total failure. I tried literally all the options for matching rules: window class, title, role, etc. — none of them worked. I’ve actually almost given up on this idea to take control of Evolution’s windows and left the rules as they were the last time I messed around with Place settings.

Eventually, what I ended up with, though, and it comes as a surprise to me as well, was a simple:

role=EShellWindow

Which was created sort of accidentally, a leftover of the code from the last time I attempted to figure out the matching rules for Evolution, but it turned out to be exactly what I needed.

So, if you need to forcibly place Evolution on its launch to any viewport, just use this rule to match the Evolution main window. That’s it. Compose Message will follow your viewport.

If you feel a little uncomfortable with Compiz’s matching rules syntax, don’t worry I felt it was a little unusual as well! Here’s a nice post, though, that breaks down the most important aspects of the window matching deal:

http://technology-flow.com/articles/control-window-behavior-compiz/

yaourt Proxy Settings

In Arch Linux, getting yaourt that is being run as normal user (which is a requirement of the program) to work with proxy was a bit of a challenge.

On my system I have proxy settings setup this way:

/etc/profile.d/proxy.sh
http_proxy=http://proxy.corp.com:8080/
ftp_proxy=http://proxy.corp.com:8080/
all_proxy=socks://proxy.corp.com:8080/
https_proxy=http://proxy.corp.com:8080/
no_proxy=localhost,127.0.0.0/8

export http_proxy;
export ftp_proxy;
export all_proxy;
export https_proxy;
export no_proxy;

When run as normal user yaourt relies on sudo to call pacman to perform package management in the system. The trouble is that when sudo is invoked by yaourt it doesn’t keep your *_proxy variables. The solution is to tell sudo to explicitly keep those variables whenever it’s invoked.

Continue reading

Suddenly /dev/null became a regular file

I was doing a routine task – preparing a new CentOS server – today and ran into quite obscure problem.

I was at the point where I needed to configure VPN link but OpenVPN wouldn’t let me daemonize itself. It complained in the logs basically saying that the problem was this:

openvpn[4738]: daemon() failed: No such device (errno=19)

That’s weird. After an hour of troubleshooting this issue on the server I took it to #openvpn@irc.freenode.net where dazo, the channel operator, pointed out that some people previously had have a similarly looking problem, and that if /dev/null was involved it might be a similar or exactly that kind of problem.

I checked /dev/null with stat utility and it was indeed just a regular file. WHOA. This is a production server that doesn’t see software updates, tested and works for the most part as a clock. Utterly inexplicable at this point to me but I don’t have time to research this right now. I just wanted to make a post about it to remember to look into this later, because this is quite interesting and doesn’t happen very often. In fact, I’ve been working with Linux for at least 5 years now, and I’ve never seen anything of the sort. Not even my more than I am experienced colleagues.

Continue reading

Keep Linux Console From Dimming The Screen

There are many ways to do it depending on your context, i.e. whether you’re working with headless setup that doesn’t need to run X server – plain old console – or with an X-based setup.

In my particular case I had  a CentOS box that ran without X server and experienced some obscure, intermittent lockups. So, I needed to hook up the display, hoping to see some output like kernel call trace or maybe some error messages the box locks up the next time. The problem was that the system would dim the screen and when the box locked up I couldn’t see anything that might had been printed on stdout.

After quite a prolonged session of error and trial, what I learned was that simply issuing setterm -blank 0 in a terminal over ssh session (didn’t try to directly enter this command, let me know if this works for you) wasn’t working.  So, what I ended up doing was adding

% echo “/usr/bin/setterm -blank 0” >> /etc/rc.local

to the end of /etc/rc.local and rebooting the box.

That did it for me (finally! lol)

OSSEC Active Response E-Mail Notifications

Here’s a very nice page that describes how to set up OSSEC active response e-mail notifications.

There’s one problem, though. In current OSSEC version 2.6 that configuration will leave you with AR rule, if once triggered, staying in loop forever. For example, if a common web attack is detected and you’ve configured OSSEC to respond with firewall drop AR, upon the timeout the offensive IP address will be deleted from the firewall configuration and re-added immediately after that. Thus, this cycle will continue endlessly.

Continue reading

Extracting a Radio Station URL from Banshee 2 SQLite DB

I’ve wanted for a while to extract a radio station url for HBR1, Ambient from Banshee 2.0.1.

My first thought was to grep recursively and disregarding character case for ‘hbr’ in $HOME. That didn’t work.

After some time I realized that probably Banshee stored most of the data in a sqlite db. I was right about that, but  finding the radio station url still wasn’t as simple.

Here’s what I did to get it printed out on my console:

~/.config/banshee-1 % sqlite3 banshee.db ‘select * from CoreTracks’ | grep -i hbr
3|2586|12|463|0|0||http://ubuntu.hbr1.com:19800/ambient.ogg||0|0|0|0|5|0|HBR1, Ambient|hbr1 ambient||,    �
!/Q    2!p�|0|0|0|0|0|0|Ambient|||||||0|0|0|0|||1294327820|1326656231|d6bb2e3f688f3bb51bbf660a6707629e|0||0
3|2587|12|464|0|0||http://ubuntu.hbr1.com:19800/tronic.ogg||0|0|0|0|5|1|HBR1, House|hbr1 house||,    �
!/,|��!|0|0|0|0|0|0|House|||||||0|0|0|0|||1294327883|1320570155|0d8fca4c8853d6f5da29987a912f0ea6|0||0
3|2588|12|465|0|0||http://ubuntu.hbr1.com:19800/trance.ogg||0|0|0|0|5|0|HBR1.com – I.D.M. Tranceponder|hbr1com – idm tranceponder||,    �
!3

Yeah, not pretty, but who cares as long as you can extract your data, right? Of course, if you’re going to re-use the output in your scripts you’ll have to figure out how to make the output prettier. If you do that, drop me a line in the comments section, please.