in reply to Peeling Data with Reserved Characters and Long Lines

Something like this?

use strict; use warnings; use autodie qw/:all/; open my $outfile, '>', 'filename.txt'; while (<>) { if ( m#-/(\w+)\?srt=# ) { print $outfile $1, "\n"; } } close $outfile;

Update: Rats, the preceding example assumes one match per line. That's not a certainty though. How about this:

use strict; use warnings; use autodie qw/:all/; open my $outfile, '>', 'filename.txt'; while ( <> ) { while( m#-/(\w+)\?srt=#g ) { print $outfile $1, "\n"; } } close $outfile;

There's another nifty way too. If you don't really care about newlines as record separators, why not call the '?srt=' your record separator instead? In that case, it would look like this:

use strict; use warnings; use autodie qw/:all/; open my $outfile, '>', 'filename.txt'; { $local $/ = '?srt='; while( <> ) { chomp; if( m#-/(\w+)$# ) { print $outfile $1, "\n"; } } } close $outfile;

Dave

Replies are listed 'Best First'.
Re^2: Peeling Data with Reserved Characters and Long Lines
by roboticus (Chancellor) on Mar 12, 2011 at 13:03 UTC

    davido:

    I really liked your third method, but you didn't use the 5-20 character bit of the specification:

    $ cat t.pl #!/usr/bin/perl use strict; use warnings; local $/ = '?srt='; while (<DATA>) { chomp; print "$1\n" if m[-/(\w{5,20})$]; } __DATA__ ...emable-Stuff-10100-PTZ-/1280640AB018292?srt=More2stuff&ha... ...wer-Idaptx-SJ10-/35DE4715844?srt=L12_Defa43Dom.. foo-/a?srt=bar;foo-/abcdefghijklmnopqrstuvwxyz?srt= foo-/abcde?srt=bar-/abcdef?srt=baz-/abcdefg?srt= $ perl t.pl 1280640AB018292 35DE4715844 abcde abcdef abcdefg

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.