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

Hi, I have a data file which contains three sections. Each section is separated by one line starting with '!' and the name of the section. The data corresponding to the section follows on the next line.
I would like to fetch the data of these three sections and put them in three different variables to refer to each one them later on.
I have tried this:
my $chunk1 =(); my $chunk2 =(); my $chunk3 =(); { local $/ = undef; ($chunck1,$chunk2,$chunk3) = split(/^\!(sectionname1|sectionname2|sect +ionname3)$/m, <>); } print "$sectionname2\n";
but that does not work :-( it gives me the the name of the first section... Obviously not what I am looking for.

Any idea ?

Thanks in advance
  • Comment on Howto separate one file into three and refer to each of these three chuncks later on
  • Download Code

Replies are listed 'Best First'.
Re: Howto separate one file into three and refer to each of these three chuncks later on
by tirwhan (Abbot) on Jun 27, 2007 at 14:34 UTC

    You're using capturing parentheses, which will cause the capture to be inserted into the split result (read perldoc split on this). Also, please use strict and use warnings, those would have caught a typo in $chunck1 as well as your incorrect use of the variable name $sectionname2 (where you actually mean $chunk2. Below a corrected version of your script

    #!/usr/bin/perl use strict; use warnings; my $chunk1; my $chunk2; my $chunk3; { local $/ = undef; (undef,$chunk1,$chunk2,$chunk3) = split(/^!(?:sectionname1|section +name2|sectionname3)$/m, <DATA>); } print $chunk2; __DATA__ !sectionname1 this is the data !sectionname2 this is more data !sectionname3 end

    All dogma is stupid.
      Thanks so much it works now !
Re: Howto separate one file into three and refer to each of these three chuncks later on
by swampyankee (Parson) on Jun 27, 2007 at 16:37 UTC

    There is, of course, More Than One Way To Do This.

    You could localize $/ to your section separator. This may cause trouble if your file is really big, as could your technique of un-defining $/.

    Or your could read the file line-by-line, switching output files as required (I would probably do this; my preference is usually in the realm of Brute Force & Ignorance).

    Or you could correct your various spelling errors, like $chunck1 for what (probably) should be $chunk1. Note, too, that when you split with capturing parentheses, as you did, the separators are included in the list split returns. I would probably write your split somewhat differently:

    my @chunks = split(/^\!(sectionname[1-9][0-9]*)$/m, <>);
    There's a couple of reasons for this. First, when I see variable names like /\$chunkΎ-9]+$/, I immediately think "this should be an array." Secondly, typing sectionname thrice, as opposed to once, adds several opportunities for typographical errors (like omitting one of the n's in sectionname), and involves typing a dozen or extra characters, and, finally, what happens when you decided to chop the file into four, five, or 137 pieces?

    emc

    Any New York City or Connecticut area jobs? I'm currently unemployed.

    There are some enterprises in which a careful disorderliness is the true method.

    —Herman Melville
Re: Howto separate one file into three and refer to each of these three chuncks later on
by shmem (Chancellor) on Jun 27, 2007 at 14:37 UTC
    First idea is that this is not the code you are running, since
    print "$sectionname2\n";

    $sectionname2 isn't defined anywhere, not to say it has to do with what split yields. Maybe you meant $chunk2?

    Then, you spell "chunck", then "chunk". There be bugs! better use an array to split those chun(c)ks into:

    @chunks = split ...

    More thibgs could be said about the code you really are running... mind to post that?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      More thibgs could be said

      shmem has a heavy cold :-)

      Cheers,
      Rob