Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: print identical keys once along with their values

by Anonymous Monk
on Nov 05, 2015 at 16:00 UTC ( [id://1147005]=note: print w/replies, xml ) Need Help??


in reply to print identical keys once along with their values

Why go to the trouble of using a hash?

#!/usr/bin/perl # http://perlmonks.org/?node_id=1146923 use strict; use warnings; $_ = join "\t", qw(1 A 1 B 2 G 2 H 2 V); 1 while s/\b((\d+)\s\S+)(.*?)\s\2\s(\S+)/$1:$4$3/; print "$_\n";

Replies are listed 'Best First'.
Re^2: print identical keys once along with their values
by bsherkhane (Initiate) on Jan 26, 2016 at 06:38 UTC
    Thank you very much, can you explain me the regex in details , like what is \2 and $1 $4 $3 special variables purpose regards, Umesh

      Hello bsherkhane,

      The escape sequences \1, \2, \3, etc., are backreferences to captures in the current regex. The special variables $1, $2, $3, etc., are likewise backreferences to the captures in the most recent regex. $1 refers to the first capture, $2 to the second capture, and so on. Captures are numbered by counting left parentheses from the left. See perlre#Capture-groups.

      The module YAPE::Regex::Explain is a useful tool for understanding regular expressions. Here is the explanation it gives for the left-hand side (i.e., the regex part) of the substitution in question:

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147005]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found