Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How do you move within an array using foreach?

by Ovid (Cardinal)
on Oct 10, 2002 at 18:34 UTC ( [id://204269]=note: print w/replies, xml ) Need Help??


in reply to How do you move within an array using foreach?

I'm a bit confused by your question as what you state in the question seems, to me, to not match the code comments. Perhaps if you were to provide sample output we might better understand. In the meantime, I am taking your question to mean "given start and end tokens in a file, how do I print out all lines from the start and end tokens?".

#!/usr/bin/perl -w use strict; my $start = qr/\Q[foo]\E/; my $end = qr/\Q[foo2]\E/; while ( <DATA> ) { if ( /$start/ .. /$end/ ) { print; } } __DATA__ ; ; foo file ; [foo] a=1 b=2 c=3 [foo2] d=4 e=5 f=6

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Re: How do you move within an array using foreach?
by flounder99 (Friar) on Oct 10, 2002 at 19:00 UTC
    Ovid's solution works but keeps scanning the file even after [foo2] is found. It is no big deal if the foo file is small but if it is huge it might take a while. I added a flag to bail out once [foo2] is found.
    #!/usr/bin/perl -w use strict; my $start = qr/\Q[foo]\E/; my $end = qr/\Q[foo2]\E/; my $flag = 0; while ( <DATA> ) { if (/$start/ .. /$end/) { $flag++; print; next; } last if $flag; } print "$flag lines printed\n"; __DATA__ ; ; foo file ; [foo] a=1 b=2 c=3 [foo2] d=4 e=5 f=6

    --

    flounder

Re (2): How do you move within an array using foreach?
by VSarkiss (Monsignor) on Oct 10, 2002 at 18:40 UTC

    That's how I read it also. If that's what the questioner is after, I have to do it often enough that I usually write it as a one-liner: perl -ne "print if /\[foo\]/../\[foo2\]/" foofile(Nothing wrong with your code, just wanted to point out a short-circuit version.)

    BTW, I used double-quotes in that example because I presume the questioner is doing this on a windows system; i.e. typing at cmd.exe or something.

Re: Re: How do you move within an array using foreach?
by P0w3rK!d (Pilgrim) on Oct 10, 2002 at 18:48 UTC
    Ovid,
    There are actually n number of tokens in the file. I was trying to simplify the question. :)

    As I read each section, I need to drop each of these into a hash.
    For example:

    # there are n number of files to parse %masterhash = ( %file_1_hash, ..., %file_n_hash); # within each file hash %file_1_hash = (%foo, %foo2); #within each section %foo = ( letter => "a", number => "b" ); # %letter would = a # %number would = b ... with n number of elements within foohash # same for foo2 %foo2 = ( %letter, %number); # same as foo #... Note: n number of sections in each file.
    Does that make sense?

    -P0w3rK!d :)

      By "section", are you referring to .ini file sections? If so, do you know about Config::IniFiles? I haven't used it, but it's a tied-hash interface that looks like it does what you want.

      If you are dealing with .ini files, but this isn't exactly the right thing, poke around CPAN; there's a lot of modules for dealing with them, so you can probably find one that does whatever you need.

        Thank you! :>

        This will save me a ton of time. The file is actually an install shield file.

      # please forgive me..there are the hashes.. %foo1 = ( a => "1", b => "2", c => "3", ); %foo2 = ( d => "4", e => "5", f => "6", );
        How about something like this then:
        #! /usr/bin/perl -w use strict; use Data::Dumper; my @lines = (<>); my %data = (); my $token = ''; foreach (@lines) { if (/\[(\w+)\]/) { $token = $1; } if (/(\w+)=(\w+)/) { $data{$token}->{$1} = $2; } } print Dumper(\%data);
        The output is:
        $VAR1 = { 'foo2' => { 'e' => '5', 'f' => '6', 'd' => '4' }, 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3' } };
        Not 100% what you want - this creates a hash of hashes - but still useable.

        JJ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found