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

Re: Newbie question, advice appreciated

by sk (Curate)
on Sep 27, 2005 at 19:56 UTC ( #495531=note: print w/ replies, xml ) Need Help??


in reply to Newbie question, advice appreciated

I am not sure if i completely understand the spec. Here is my shot at this. I have put the data at the end so you can see if it is structured in that way.

#!/usr/bin/perl -w use strict; # open(DATA,"file.txt") or die $!; # I am using DATA handle but you c +an open a file to read contents too. my %functions; while (<DATA>) { chomp; my @functxt = (); my $desc = (); if (/STANDARD/) { @functxt = split /\s+/; $desc = <DATA>; $functions{pop(@functxt)} = $desc; # assuming last word is +function name } } print +($_, " : ", $functions{$_}, $/) for (keys %functions); __DATA__ # STANDARD func1 func1 does something funny blah blah ok junk text # STANDARD func2 func2 does something useful # STANDARD func3 func3 is a math function

output

func1 : func1 does something funny func3 : func3 is a math function func2 : func2 does something useful +


Comment on Re: Newbie question, advice appreciated
Select or Download Code
Re^2: Newbie question, advice appreciated
by sauoq (Abbot) on Sep 27, 2005 at 23:46 UTC
    @functxt = split /\s+/;

    Usually, that's not the pattern you want to use with split. Given the data format specification we were given in the above problem, it wouldn't make a difference, but split ' ' (or just split with no arguments at all) is usually what is really wanted. The difference is that \s+ can produce a null field when there is leading whitespace.

    -sauoq
    "My two cents aren't worth a dime.";
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2015-07-05 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls