You are not logged in.

#1 2019-02-17 18:58:49

madscience
Member
From: Ontario, Canada
Registered: 2014-02-11
Posts: 74
Website

[SOLVED] How to strip characters from perl script output?

Hello fellow Archers,

I'm currently using the following perl script to display my i3 desktops in lemonbar.  I copied this from someone's github, and I don't know perl at all.
In my i3 config, the desktops are named eg: "1: code".  What's the simplest way in perl to strip the leading number and colon from the output?

#!/usr/bin/env perl
# vim:ts=4:sw=4:expandtab:ft=perl
#
# Print i3 workspaces on every change.
#
# Format:
#   For every workspace (x = workspace name)
#       - "FOCx" -> Focused workspace
#       - "INAx" -> Inactive workspace
#       - "ACTx" -> Ative workspace
#       - "URGx" -> Urgent workspace
#
# Uses AnyEvent I3 0.8 -> https://metacpan.org/module/AnyEvent::I3
# Based in i3-wsbar of Michael Stapelberg -> http://code.stapelberg.de/git/i3/tree/contrib/i3-wsbar
#
# 16 feb 2015 - Electro7

use strict;
use warnings;
use AnyEvent::I3;
use AnyEvent;
use v5.10;
use open ':std', ':encoding(UTF-8)';

my $socket_path = undef;
my ( $workspaces, $outputs ) = ( [], {} );
my $w = AnyEvent->timer(
    after => 3,
    cb    => sub {
        die "Connection to i3 timed out. Verify socket path ($socket_path)";
        exit 1;
    }
);

my $i3 = i3($socket_path);

# Disable buffering
$| = 1;
STDERR->autoflush;
STDOUT->autoflush;

# Wait a short amount of time and try to connect to i3 again
sub reconnect {
    print "reconecting\n";
    my $timer;
    $i3 = i3($socket_path);
    if ( !defined($w) ) {
        $w = AnyEvent->timer(
            after => 3,
            cb    => sub {
                die
"Connection to i3 timed out. Verify socket path ($socket_path)";
                exit 1;
            }
        );
    }

    my $c = sub {
        $timer = AnyEvent->timer(
            after => 0.01,
            cb    => sub { $i3->connect->cb( \&connected ) }
        );
    };
    $c->();
}

# Connection attempt succeeded or failed
sub connected {
    my ($cv) = @_;

    if ( !$cv->recv ) {
        reconnect();
        return;
    }

    $w = undef;

    $i3->subscribe(
        {
            workspace => \&ws_change,
            output    => \&output_change,
            _error    => sub { reconnect() }
        }
    );
    ws_change();
    output_change();
}

# Called when a ws changes
sub ws_change {

    # Request the current workspaces and update the output afterwards
    $i3->get_workspaces->cb(
        sub {
            my ($cv) = @_;
            $workspaces = $cv->recv;
            update_output();
        }
    );
}

# Called when the reply to the GET_OUTPUTS message arrives
sub got_outputs {
    my $reply = shift->recv;
    my %new = map { ( $_->{name}, $_ ) } grep { $_->{active} } @{$reply};

    for my $name ( keys %new ) {
        $outputs->{$name} = $new{$name};
    }

    update_output();
}

sub output_change {
    $i3->get_outputs->cb( \&got_outputs );
}

sub update_output {
    my $out;

    for my $name ( keys %{$outputs} ) {
        $out .= "WSP";

        for my $ws ( @{$workspaces} ) {
            my $state = "INA";
            $state = "ACT" if $ws->{visible};
            $state = "URG" if $ws->{urgent};
            $state = "FOC" if $ws->{focused};
            my $name = $ws->{name};
            $out .= qq|$state$name |;
        }

        $out .= "\n";

        print $out;
    }
}

$i3->connect->cb( \&connected );

# let AnyEvent do the rest ("endless loop")
AnyEvent->condvar->recv

Last edited by madscience (2019-02-17 20:06:34)

Offline

#2 2019-02-17 19:47:27

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,260
Website

Re: [SOLVED] How to strip characters from perl script output?

Why would you copy someone else's script that does not do what you want in a language you do not understand then attempt to edit it.  Just write your own in a language you can use.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2019-02-17 19:53:38

madscience
Member
From: Ontario, Canada
Registered: 2014-02-11
Posts: 74
Website

Re: [SOLVED] How to strip characters from perl script output?

I'm using it because it works perfectly, and I can follow what it's doing when I read it.  I'm just interested in making a simple change to the output format, and I don't want to rewrite this in shell script.

Offline

#4 2019-02-17 19:58:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,260
Website

Re: [SOLVED] How to strip characters from perl script output?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2019-02-17 20:05:48

madscience
Member
From: Ontario, Canada
Registered: 2014-02-11
Posts: 74
Website

Re: [SOLVED] How to strip characters from perl script output?

Thanks, I just found a similar link on stackoverflow and figured it out by adding:

        $out =~ s/\d://g;

before:
        $out .= "\n";

And I know I should learn perl if I'm using a script written in it.  Thanks!

Offline

#6 2019-02-17 20:12:33

agrewal
Member
Registered: 2018-11-23
Posts: 20

Re: [SOLVED] How to strip characters from perl script output?

madscience wrote:

        $out =~ s/\d://g;

$out =~ s/\d: //;

is probably what you want. That g flag will cause you unexpected frustration in the future.

Offline

#7 2019-02-17 21:19:30

madscience
Member
From: Ontario, Canada
Registered: 2014-02-11
Posts: 74
Website

Re: [SOLVED] How to strip characters from perl script output?

agrewal wrote:
madscience wrote:

        $out =~ s/\d://g;

$out =~ s/\d: //;

is probably what you want. That g flag will cause you unexpected frustration in the future.

My understanding is that I need the //g to apply the regex globally to the string.  If I remove it, it only removes the first number and colon it finds in the output.  If I avoid using numerics in the names of my workspaces, it should be OK right?

Offline

#8 2019-02-18 00:51:55

agrewal
Member
Registered: 2018-11-23
Posts: 20

Re: [SOLVED] How to strip characters from perl script output?

I'm sorry. Yes, you need the g flag, but it'll strip all occurrences of a number followed by a colon. As long as you don't plan to use that in a name, you'll be fine.

Offline

Board footer

Powered by FluxBB