0

I am trying to remove the first column (and in general the n-th column) from a table with Mojolicious. I currently do it like this:

$table->find('td:first')->each(sub { my ($e) = @_; $e->remove });

but this isn't working.

What am I doing wrong?

(Yeah I know I am asking a lot of questions on Mojolicious - just starting, and it's quite cool)

simone
  • 4,667
  • 4
  • 25
  • 47

1 Answers1

0

There is no CSS Selector for :first.

The following uses :nth-child(1) to isolate the first column and then removes the cells:

Could also have used :first-child, :first-of-type, etc. Be sure to check out all the different selectors available to be sure which ones will serve your purpose best.

use strict;
use warnings;

use Mojo::DOM;

my $dom = Mojo::DOM->new(do {local $/; <DATA>});

$dom->find('tr > td:nth-child(1)')->each( sub {$_->remove} );    

print $dom;

__DATA__
<html>
<head>
<title>Hello Tables</title>
</head>
<body>
<h1>Hello world</h1>
<table>
<tr><td>Row 1 Col 1</td><td>Row 1 Col 2</td><td>Row 1 Col 3</td></tr>
<tr><td>Row 2 Col 1</td><td>Row 2 Col 2</td><td>Row 2 Col 3</td></tr>
<tr><td>Row 3 Col 1</td><td>Row 3 Col 2</td><td>Row 3 Col 3</td></tr>
<tr><td>Row 4 Col 1</td><td>Row 4 Col 2</td><td>Row 4 Col 3</td></tr>
</table>
</body>
</html>

Outputs:

<html>
<head>
<title>Hello Tables</title>
</head>
<body>
<h1>Hello world</h1>
<table>
<tr><td>Row 1 Col 2</td><td>Row 1 Col 3</td></tr>
<tr><td>Row 2 Col 2</td><td>Row 2 Col 3</td></tr>
<tr><td>Row 3 Col 2</td><td>Row 3 Col 3</td></tr>
<tr><td>Row 4 Col 2</td><td>Row 4 Col 3</td></tr>
</table>
</body>
</html>
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Miller
  • 34,962
  • 4
  • 39
  • 60
  • CSS2 has a [`first-child` pseudo-class](http://www.w3.org/TR/CSS2/selector.html#first-child) – Borodin Aug 27 '14 at 23:15
  • Aye, I mentioned and linked to that in my 3rd sentence. The `first` he was using I suspect was drawn from the method call available in `Mojo::DOM` and therefore not actually a CSS Selector like he was using it. – Miller Aug 27 '14 at 23:28