-1

I want to simply take all of the characters until the end of the line, and place these in a variable. Here is a try which might explain it more:

open my $in, '<', 'in.txt';
$iteration = 0
while(<$in>){
    chomp;
    next if /^#|^$/;  //Skip over lines starting with # or empty lines.
    if($iteration==0) {
        die "Error in file formatting!\n" unless /^Data1: [.]*/; //*****
        my ($data1) = /Data[1]: ([.]*)/;  //*****
        $iteration++;
    }
    else if($iteration==1) {
        die "Error in file formatting!\n" unless /^Data2: \d+/;
        my ($data2) = /Data[2]: (\d+)/;
        $iteration++;

    }
    else if($iteration==2) {
        die "Error in file formatting!\n" unless /^Data3: \d+/;
        my ($data3) = /Data[3]: (\d+)/;
        $iteration=0;

    }
}

In the first if statement inside the loop, this looks for the string "Data[1]: " at the beginning of the line, and now I want to take everything after that, and place it in a variable. So, spaces, punctuation, numbers, underscores, etc are all fine, but as soon as a new line comes, I want to move on to the next line in my loop so stop there (I want to stop at the end of the line).

Musicode
  • 661
  • 1
  • 12
  • 29
  • 1
    `next if /\s*(?:#|$)/` does not skip empty lines or lines that start with `#`. It skips all lines that have `#` in them, and all lines that have a line ending. In short: all lines. – TLP Nov 05 '14 at 00:29
  • @TLP Actually, this is really helpful, as I think this solves my question as well. How would I look to see if it begins with a # and then go to the end of the line? This is essentially what I need to do in my question, as well. – Musicode Nov 05 '14 at 00:33
  • `[a-Z]` is an invalid range. – TLP Nov 05 '14 at 00:33
  • 1
    Actually? It was meant to be helpful. If you want to skip lines that begin with `#` you need to anchor to the beginning of line with `^`: `/^#/`, or `/^\s*#/` to allow optional whitespace. – TLP Nov 05 '14 at 00:35
  • Thanks very much TLP. I think I'm understanding all of the options for regular expressions a lot more now. – Musicode Nov 05 '14 at 00:45

1 Answers1

0

/[.]+/ matches one or more .. You want /.*/, which matches any number of characters except newlines.

my ($data1) = /^Data\[1\]: (.*)/
   or die "Syntax error\n";
ikegami
  • 367,544
  • 15
  • 269
  • 518