Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Extract info from a list

by Anonymous Monk
on Jul 16, 2003 at 01:21 UTC ( [id://274653]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Please advise a reg expression to extract the information right after the "//" in the below list example (my real list has about 400 lines of information):
//word.one.team/other.stuff/Info.info //this.sentence/other.inforation/moreInfo //first.part.here/set.list //this.sentence/data/processing
I need to get this from the above list and if possible how many occurences of each:
word.one.team = 1 this.sentence = 2 first.part.here = 1

Replies are listed 'Best First'.
Re: Extract info from a list
by Zaxo (Archbishop) on Jul 16, 2003 at 01:36 UTC

    An easy way is to split on '/' which ordinarily needs to be escaped. The resulting array has nothing in the first two slots,

    my %count; $count{(split /\//)[2]}++ while <DATA>; printf "%s = %d\n", $_, $count{$_} for keys %count; __DATA__ //word.one.team/other.stuff/Info.info //this.sentence/other.inforation/moreInfo //first.part.here/set.list //this.sentence/data/processing

    Update: ++tos for the correction. Repaired.

    After Compline,
    Zaxo

      a little typo in your fine solution

      $_ and $count{$_} have to be swapped

      printf "%s = %d\n", $_, $count{$_} for keys %count;
      greetings, tos
Re: Extract info from a list
by bobn (Chaplain) on Jul 16, 2003 at 01:29 UTC
    # UN-TESTED for { @list ) { next unless m!^//(.*?)/!; $seen{$1}++; } for ( sort keys %seen ) { print "$_ = $seen{$_}\n"; }


    --Bob Niederman, http://bob-n.com
Re: Extract info from a list
by tedrek (Pilgrim) on Jul 16, 2003 at 03:31 UTC

    Actually you don't even need a regex for this:

    $s{substr($_, 2,index($_,'/',2)-2)}++ for <DATA>; print reverse sort map {"$_ = $s{$_}\n"} keys %s; __DATA__ //word.one.team/other.stuff/Info.info //this.sentence/other.inforation/moreInfo //first.part.here/set.list //this.sentence/data/processing

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-16 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found