split trouble

D

dima

Very easy problem:

my $pair = "var=value";
($fake,$value) = split/=/,$pair;

I want get only value, but not var!
How can I do that else,without $fake variable?
 
P

Peroli

Hi,

Very easy problem:

my $pair = "var=value";
($fake,$value) = split/=/,$pair;

I want get only value, but not var!
How can I do that else,without $fake variable?

Try this...

#!/usr/bin/perl
use warnings;
use strict;

my $pair = "var=value";
$pair =~ s/[^=]+=//;
print $pair;

- Peroli Sivaprakasam
 
G

George

Very easy problem:

my $pair = "var=value";
($fake,$value) = split/=/,$pair;

I want get only value, but not var!
How can I do that else,without $fake variable?

(split/=/,$pair)[1]
 
A

Anno Siegel

Peroli said:
Hi,

Very easy problem:

my $pair = "var=value";
($fake,$value) = split/=/,$pair;

I want get only value, but not var!
How can I do that else,without $fake variable?

Try this...

#!/usr/bin/perl
use warnings;
use strict;

my $pair = "var=value";
$pair =~ s/[^=]+=//;
print $pair;

Yes, that works, but I wouldn't recommend the solution. For one, it
destroys the entry variable $pair, and you end up with the result
in an inappropriately named variable. Quite generally, if you want to
extract text from a string, destroying the unwanted part is only
justified if the unwanted part is much easier to match than the wanted
part. That isn't the case here. So, if you want a regex solution,
catch everything after the first "=":

my ( $value) = $pair =~ /=(.*)/;

With split, the unwanted variable name can be skipped like this:

my ( undef, $value) = split /=/, $pair;

or

my $value = ( split /=/, $pair)[ 1];

I prefer the second. Code untested.

Anno
 
J

Jürgen Exner

Very easy problem:

my $pair = "var=value";
($fake,$value) = split/=/,$pair;

I want get only value, but not var!
How can I do that else,without $fake variable?

(undef,$value) = split/=/,$pair;

jue
 
J

Joe Smith

my $pair = "var=value";
($fake,$value) = split/=/,$pair;

What about "var=value=more"? Do you want "value" or "value=more"?

$short_val = (split /=/,$pair)[1];
$long_val = (split /=/,$pair,2)[1];

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top