How can I extract the values ​​after = in my string using Perl?

I have a line like this

field1=1 field2=2 field3=abc

I want to output it as

2,1,abc

Any ideas on how I can do this? I can write a small C or Java program for this, trying to find an easy way to do this in Perl.

+3
source share
11 answers
#!/usr/bin/perl

use strict;
use warnings;

# Input string
my $string = "field1=1 field2=2 field3=abc";
# Split string into a list of "key=value" strings
my @pairs = split(/\s+/,$string);
# Convert pair strings into hash
my %hash = map { split(/=/, $_, 2) } @pairs;
# Output hash
printf "%s,%s,%s\n", $hash{field2}, $hash{field1}, $hash{field3};   # => 2,1,abc
# Output hash, alternate method
print join(",", @hash{qw(field2 field1 field3)}), "\n";
+5
source
use strict;
use warnings;

my $string = 'field1=1 field2=2 field3=abc';
my @values = ($string =~ m/=(\S+)/g);
print join(',', @values), "\n";
+6
source

m//g :

#!/usr/bin/perl

use strict;
use warnings;

my $x = "field1=1 field2=2 field3=abc";

if ( my @matches = $x =~ /(?:field[1-3]=(\S+))/g ) {
    print join(',', @matches), "\n";
}

__END__

:

C:\Temp> klm
1,2,abc
+2
 $_='field1=1 field2=2 field3=abc';
 $,=',';
 say /=(\S+)/g

Perl golf: D

+2

:

my $s = "field1=1 field2=2 field3=abc";
$s =~ /field1=(\w*) field2=(\w*) field3=(\w*)$/; //pick out each field
print $1,$2,$3;'
12abc
my $s = "field1=1 field2=2 field3=abc"; 
my @arr = split / /, $s; print @arr,"\n"; //make an array of name=value pairs
my @vals = map { @pairs = split /=/, $_; $pairs[1] } @arr;  //get the values only from each pair
print @vals'
field1=1field2=2field3=abc
12abc
  • ( , )
my $s = "field1=1 field2=2 field3=abc"; 
my @arr = split / /, $s;  
my %pairs = map { split=/, $_; } @arr; 
print $pairs{field1}, $pairs{field2}, $pairs{field3}
12abc
+1
my $str = 'field1=1 field2=2 field3=abc';
print(join(',', map { (split('=', $_))[1] } split(' ', $str)));
+1

, :


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

my $str='a=1 b=2 c=abc';
my @v;
while ($str =~ /=(\S+)/g) {
    push @v, $1;
}
print join (',', @v);
0

Perl .


#! /usr/bin/perl

$str = "field1=1 field2=2 field3=abc";
$str =~ /field1=(\S+)\ field2=(\S+)\ field3=(\S+)/;
print "$1,$2,$3", "\n";
0
source
my $a = "field1=1 field2=2 field3=abc";
my @f = split /\s*\w+=/, $a;
shift(@f);
print join(",", @f), "\n";
0
source
$string="field1=1 field2=2 field3=abc";
@s=split /\s+/,$string;
$temp=$s[1];$s[1]=$s[0];$s[0]=$temp;
foreach (@s){s/.*=//; push(@a,$_ );}
print join(",",@a);
0
source

If you really need both keys and values. I would put them in a hash. You can just grab both sides of the " =" and paste directly into the hash.

use strict;
use warnings;

my $str = 'field1=1 field2=2 field3=abc';

my %fields = $str =~ / (\S+) \s* = \s* (\S+) /xg;

use YAML;
print Dump \%fields
---
field1: 1
field2: 2
field3: abc

For more information, please read perldoc perlre.

If you are just a beginner, you can read perldoc perlretut.

0
source

Source: https://habr.com/ru/post/1712317/


All Articles