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

Hello Monks, I have this small input file that I am trying to read from my perl script , it looks like this :
[CODE] 1000 [PASS] AMY AMY2376G NOOR NOO2376G MONK MON2376G
I need to read that file and get the following variables sat :
$code = 1000 $user1 = AMY $pass1 = AMY2376G $user2 = NOOR $pass2 = NOO2376G . . etc
I don't seem to grep what i need correctly , I am trying somthing like :
open ( input , "../input" ) or die " .... "; while (<input>) { my $code=$_ if /^CODE/ . . . }
just not sure how to get to the second line after getting the thing I am looking for ,, can someone help. thanks

Replies are listed 'Best First'.
Re: reading an input file
by Vautrin (Hermit) on Jan 22, 2004 at 22:06 UTC

    First off, the regular expression you have my $code=$_ if /^CODE/ will never match anything because the caret (^) means if what follows is at the very beginning of the line, so it is looking for a line which starts with the word "CODE", not "[CODE]" (remember [ and ] are used in regular expressions so they need to be escaped like another poster suggested: m/^\[CODE\]/

    Secondly, is the number of users of variable length? You might be better off storing users as a hashref in an array, so you can access user 0 as $array[0] (or $array->[0] if you're using an array reference)

    Third, why don't you have the following at the top of your script:

    use warnings; use strict;
Re: reading an input file
by duff (Parson) on Jan 22, 2004 at 22:46 UTC

    Here's an (untested) implementation of what I think you want:

    my ($section, $code, %passwds); while (<>) { chomp; if (/^\[(.*?)\]$/) { $section = $1; next; } if ($section eq 'CODE') { $code = $_; } elsif ($section eq 'PASS') { my ($name,$pass) = split; $passwds{$name} = $pass; } }

    This actually makes a hash to map the names to passwords rather than creating a bunch of scalars. There's other ways you could do it too.

      duff , I don't know much about hash , how would I print the output ? thanks

        Reading perldata should give you some more clues about perl's built-in data structures (like hashes). To print the contents of the hash, you could do something like this:

        for my $user (keys %passwds) { print "$user $passwds{$user}\n"; # outputs user and password }

        In the hash I created in the original snippet of code I gave the %passwds hash held the usernames and passwords. Usernames were the keys of the hash, passwords were the values. This loop just iterates over the keys and outputs each key (user) and it's associated value (password).

      I think you want something in there to ignore (or reset $section upon encountering) blank lines. Otherwise, the blank like after the code value will cause $code to be made empty. Of course, a non-blank, non-section line will do that also. For good error checking, you'd have to do something like this:
      my ($section, $code, %passwds); while (<>) { chomp; undef $section, next unless /\S/; # next line is equivalent to duff's; just showing a # different WTDI next if ($section) = /^\[(.*?)\]$/; if ($section eq 'CODE') { warn "overriding previous CODE" if defined $code; $code = $_; # CODE section automatically ends after one line undef $section; } elsif ($section eq 'PASS') { if ((my ($name, $pass) = split) != 2) { warn "bad input $_ ignored"; next; } $passwds{$name} = $pass; } else { warn "unexpected input $_; possibly invalid section $section"; } }
Re: reading an input file
by pzbagel (Chaplain) on Jan 22, 2004 at 21:49 UTC

    Try:

    my $code; if (/^\[CODE/) { #Grab the next line from <input> $code=<input>; #Don't forget to chomp $code; }

    Later

Re: reading an input file
by daddyefsacks (Pilgrim) on Jan 22, 2004 at 21:52 UTC
    this would probably do the trick

    Update: or perhaps I should actually read the post before replying :) Sorry about that, don't listen to me.