Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Newbie question, advice appreciated

by sk (Curate)
on Sep 27, 2005 at 19:56 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://495531]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found