Reputation: 35
I have string like this:
in.a+in.b=in.c
I want to put the characters that are next to the dot (i.e., a
and b
) on the left-hand side in one list and the other character, which is on the right-hand side (i.e., c
) in another list. Can you please help me with the code?
#!/usr/bin/perl
use strict;
use warnings;
my $string="in.a+in.b=in.c";
my @chars=split("=",$string);
print "First: @chars";
Note: the "equal to" operator will be there, but any number of characters can be there on the left-hand or right-hand side. So I just want to put the characters that are next to the .
in one list.
Upvotes: 2
Views: 114
Reputation: 386676
my ($lhs, $rhs) = split(/=/, $string);
my @lhs_leaves = $lhs =~ /\.(\w+)/g;
my @rhs_leaves = $rhs =~ /\.(\w+)/g;
Upvotes: 0
Reputation: 6553
my $string = "in.a+in.b=in.c";
my @parts = map { [ /\.(\w+)/g ] } split(/=/, $string);
The bits on the left-hand side will be in $parts[0]
, and the bits on the right-hand side will be in $parts[1]
. Here's a Data::Dumper representation of the result:
$VAR1 = [
[
'a',
'b'
],
[
'c'
]
];
And here's a usage example:
print "Left-hand side: @{$parts[0]}\n";
print "Right-hand side: @{$parts[1]}\n";
Output:
Left-hand side: a b
Right-hand side: c
Upvotes: 2
Reputation: 242343
Use a regex to extract the strings from the left and right hand sides.
#! /usr/bin/perl
use warnings;
use strict;
my $string = 'in.a+in.b=in.c';
my ($lhs, $rhs) = split /=/, $string;
my @left = $lhs =~ /\.(\w+)/g;
my @right = $rhs =~ /\.(\w+)/g;
print "Left: @left.\nRight: @right.\n";
Upvotes: 2