grosseskino
grosseskino

Reputation: 11

Perl: How do I split AND cut a given String?

I have a String that looks like this:

$string = "Tags: sweet, yummie, chocolate, dark"

I want to insert these Tags in a Mysql table.

Upvotes: 0

Views: 902

Answers (3)

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

There is a split function in Perl.

The split function is used to split a string into smaller sections. You can split a string on a single character, a group of characers or a regular expression (a pattern).

You can also specify how many pieces to split the string into.

Upvotes: 0

Toto
Toto

Reputation: 91385

If your Tags always end with column :, you could do somethong like:

#!/usr/bin/perl
use Modern::Perl;
use Data::Dumper;

my $string = "Tags: sweet, yummie, chocolate, dark";
my @parts = split/[:,]\s*/,$string;
say Dumper \@parts;

output:

$VAR1 = [
          'Tags',
          'sweet',
          'yummie',
          'chocolate',
          'dark'
        ];

Upvotes: 0

Tim
Tim

Reputation: 14154

Start by removing the "Tags: " with a regex, and then split on ", ".

my $string = "Tags: sweet, yummie, chocolate, dark"
$string =~ s/Tags: //;
my @tags = split /, /, @string;

For the MySQL connection, you could use DBI::MySQL.

Upvotes: 3

Related Questions