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

Extract info from a list

by Anonymous Monk
on Jul 16, 2003 at 01:21 UTC ( #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):
// //this.sentence/other.inforation/moreInfo // //this.sentence/data/processing
I need to get this from the above list and if possible how many occurences of each: = 1 this.sentence = 2 = 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__ // //this.sentence/other.inforation/moreInfo // //this.sentence/data/processing

    Update: ++tos for the correction. Repaired.

    After Compline,

      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,
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__ // //this.sentence/other.inforation/moreInfo // //this.sentence/data/processing

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2021-12-02 17:05 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (23 votes). Check out past polls.