Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

delimiters anyone??

by deriwana (Initiate)
on Jun 16, 2001 at 18:26 UTC ( [id://89049]=perlquestion: print w/replies, xml ) Need Help??

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

I am wanting to create a script that edits a flat-file
database using double colons (::)as delimiters to separate the entries..i.e.,
::
here is a single entry with
many lines of html code to be edited
::
I need to set up an administration page (user friendly text box type of thing) to do the editing over the web
What expressions would accomplish this?

Replies are listed 'Best First'.
Re: delimiters anyone??
by srawls (Friar) on Jun 16, 2001 at 18:32 UTC
    Check out split. You would use it like this:
    my @entries = split/::/,$file;

    The 15 year old, freshman programmer,
    Stephen Rawls

Re: delimiters anyone??
by damian1301 (Curate) on Jun 16, 2001 at 23:26 UTC
    You would slurp the whole file in a scalar variable (there are two ways to do this: ($file) = <FH>; or undef $/; $file = <FH>;). Then you just follow srawl's advice and split it on '::':
    @things = split/::/$file;

    Then you have in $things[0] the HTML to be edited, and the other things it will split on.

    Tiptoeing up to a Perl hacker.
    Dave AKA damian

Re: delimiters anyone??
by ariels (Curate) on Jun 17, 2001 at 11:34 UTC
    Try setting $/="::" or $/="::\n" before reading in your file, to change Perl's idea of what a "line" is to "something ending in `::'".
Re: delimiters anyone??
by jeroenes (Priest) on Jun 18, 2001 at 10:25 UTC
    Are you sure about the flat-file approach? That way you always have to read/write the whole thing when editing a single item like:
    #!/usr/bin/perl -i #first arg is the item_number, second the text-file with replacement #dbase file on STDIN, edited file on STDOUT my $item_number = $ARGV[0]; open IN, $ARGV[1] or die "Could not open $ARGV[1]: $!"; my $replacement; { local $/ = undef; $replacement = <IN>; } { local $/ = '::'; while( <> ) { $_ = $replacement if $. == $item_number; print; print '::' if not eof; } }
    When the dbase gets large, the overhead may become problematic. Consider a RDBM like postgres or a good DB like BerkeleyDB.

    Cheers,

    Jeroen
    "We are not alone"(FZ)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found