Re: Splitting Large SQL Script Into Several; Concat Hash Doesn't Match
On Tue, Feb 14, 2012 at 12:12:07PM -0500, Brandon McCaig wrote: The program follows (also attached in case my wrapping for mail breaks something): I knew I would forget to attach it. :-X This time hopefully I remember.. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' #!/usr/bin/perl use 5.010; use strict; use utf8; use warnings; use Data::Dumper; use IO::Handle; use Getopt::Long qw/GetOptionsFromArray HelpMessage :config auto_help bundling no_auto_abbrev no_getopt_compat no_ignore_case_always no_require_order permute/; my $batch_keyword = 'GO'; my $output = 'out.sql'; my $max_size = '10M'; GetOptions( 'keyword|K=s' = \$batch_keyword, 'output|O=s' = \$output, 'max-size|S=s' = \$max_size, ); $max_size = parse_stream_size($max_size); my ($sql_file) = @ARGV; open my $fh, ' :encoding(utf-8)', $sql_file or die input: $!; my $i = 0; my $buf = ''; my $next_batch = ''; while(my $line = $fh) { $next_batch .= $line; chomp $line; if($line =~ /^\Q$batch_keyword\E$/) { if(length($buf) + length($next_batch) $max_size) { open my $out_fh, ' :encoding(utf-8)', next_file($output, $i++) or die output: $!; $out_fh-print($buf); close $out_fh or warn output: $!; $buf = ''; } $buf .= $next_batch; $next_batch = ''; } } close $fh or warn input: $!; sub next_file { my ($orig, $i) = @_; my $format; if($orig =~ /\.sql$/) { ($format = $orig) =~ s/\.sql$/_%04d.sql/; } else { $format = $orig . '_%04d'; } return sprintf $format, $i; } sub parse_stream_size { my ($size) = @_; my ($num, $mult) = $size =~ /((?:[0-9]+)?\.?(?:[0-9]+)?)\s*([BKMG])?/i; die invalid stream size '$size' unless defined $num $num 0; my %exp = ('B' = 0, 'K' = 1, 'M' = 2, 'G' = 3); my $exp = $exp{uc($mult // 'B')}; $size = $num * 1024 ** $exp; return $size; } signature.asc Description: Digital signature
Re: Splitting Large SQL Script Into Several; Concat Hash Doesn't Match
Hi Brandon, On Tue, 14 Feb 2012 12:12:07 -0500 Brandon McCaig bamcc...@gmail.com wrote: Hello: tl;dr: A program indended to split a large MS SQL script into smaller ones appears to be losing data. See below for the script. I guess that maybe I'm making a silly mistake with newlines or file encoding or something.. My colleague is working with a relatively large generated SQL script that Visual Studio doesn't seem to be able to handle. The script is approximately 370 MB[1] in UTF-16 Little Endian (the default encoding of whatever generated the script; likely SQL Server Management Studio). Converted to UTF-8 (using Vim) it's unsurprisingly about half of that[2]. There are various ways to solve this. My colleagues decided to manually split the file up into manageable pieces, but that seemed tedious to me. I opted to hack up a Perl program to split it up for me. Initially (perhaps with insufficient thought) I decided to split it up into batches. I don't really know the details, but MS SQL has a batch keyword, by default 'GO', that it uses to split scripts up. I don't really know what it means or why it's used, but I know that it is necessary at times.. I figured this was the most reliable boundary to split on so the scripts would still have a hope of being executable without errors. Anyway, within 30 minutes or so I had written a Perl program to split on this boundary (there are probably easier ways, in hindsight, albeit from Windows those ways might not be as easy as on Unix-like platforms). That resulted in some 12000+ files, which was obviously not very practical to be manually executed by a human. It might suffice to automatically execute the batches one by one, but I have practically no experience communicating with DBMSes with Perl (or with that much SQL at a time) and I wouldn't be able to convince my colleagues to give me a chance to try. It could also potentially be dangerous if I get something wrong so I don't know if I'd really want to rush such a solution anyway, but I digress. In order to cut the number of files down in size I added a size parameter to the program and modified it to bundle batches into files up to that maximum size each. The program appears to work at first glance, producing approximately the right number of files of approximately the right sizes, and terminating on batch boundaries (by default, a line containing only 'GO'). I felt triumphant, but realized that I should try to validate that the split scripts are exactly equivalent to the single, extra large script. I decided to calculate SHA1 and MD5 hashes for the original SQL script and a concatenation of the split SQL files. AFAIK, those hashes should match if my Perl program and concatenation were done properly. Unfortuantely, the SHA1 does not match. I used MSYS cat to do the concatenation, and MSYS sha1sum to calculate the hashes: C:\Users\bamccaig\src\projectXcat data/large.sql | sha1sum [sha1sum of large.sql spit out] C:\Users\bamccaig\src\projectXcat data/out*.sql | sha1sum [sha1sum of the concatenation of data/out_.sql - data/out_0016.sql] These hashes don't match and now I'm stuck trying to figure out why or abandoning this program and leaving my colleague to do it by hand. Even if we don't use this program to solve this problem I would like to find my mistake(s) and correct them. What I would do is write another program (possibly in Perl) to compare the differences and report immediately the location within the two files, and the context before it and after it. It may be a \r\n vs. \n problem or it may be something more subtle. Make sure you open the files in a binmode $fh, 1 mode so it will read binary data. It may also be variations on Unicode encodings, which would be harmless. Regards, Shlomi Fish [SNIPPED] -- - Shlomi Fish http://www.shlomifish.org/ My Public Domain Photos - http://www.flickr.com/photos/shlomif/ Tel Aviv, a functional definition: free parking space‐free space. — Shachar Shemesh ( http://blog.shemesh.biz/?p=435 ) Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Splitting Large SQL Script Into Several; Concat Hash Doesn't Match
On Tue, Feb 14, 2012 at 07:59:18PM +0200, Shlomi Fish wrote: Hi Brandon, Hi Shlomi: tl;dr: Fixed. See bottom of message for diff. What I would do is write another program (possibly in Perl) to compare the differences and report immediately the location within the two files, and the context before it and after it. It may be a \r\n vs. \n problem or it may be something more subtle. Make sure you open the files in a binmode $fh, 1 mode so it will read binary data. It may also be variations on Unicode encodings, which would be harmless. Your encouragement got me to thinking about this. I was envisioning a simple C program that would read one byte at a time from the sets of files until it found an error and then output the offset and each byte's value. While thinking about that and evaluating how effective it would be debugging it, I began to think about my mail message and the lack of test data for anybody else to try the program with. I quickly concluded that anybody could test the program by creating a file with lorem ipsum delimited by lines containing only GO, or whatever the -K or --batch-keyword option was set to. This got me to thinking that I should be testing this program with a much smaller input. That would make it much easier to test with. I originally tried to use MSYS diff to see the differences, but the files were too large for it to handle (diff: memory exhausted). I created a small sample file to test with: SELECT * FROM table1; GO SELECT * FROM table2; GO INSERT INTO table3 (x, y, z) VALUES (1, 2, 3); I ran the program and diffed the original with the concatenation of the output and saw that an entire batch was missing (the INSERT statement). I immediately recognized that unless the input file ended with a batch keyword (and whitespace) some of the data would be unwritten to output files! I modified the program to fix this, and after debugging that and getting it working as expected, I tried again with the large input file. I hashed the results again and it matches! Success! Thanks for getting me going, Shlomi! :) The diff follows[1] (the fixed program is attached[2]). diff -r 66d9870b3f36 -r e1df0178d049 data/split_batches.pl --- a/data/split_batches.pl Tue Feb 14 12:50:09 2012 -0500 +++ b/data/split_batches.pl Tue Feb 14 14:04:34 2012 -0500 @@ -40,10 +40,7 @@ { if(length($buf) + length($next_batch) $max_size) { -open my $out_fh, ' :encoding(utf-8)', -next_file($output, $i++) or die output: $!; -$out_fh-print($buf); -close $out_fh or warn output: $!; +$i++ if write_file(next_file($output, $i), $buf); $buf = ''; } $buf .= $next_batch; @@ -51,6 +48,9 @@ } } +$buf .= $next_batch; +write_file(next_file($output, $i++), $buf); + close $fh or warn input: $!; sub next_file @@ -85,3 +85,13 @@ return $size; } +sub write_file +{ +my ($name, $buf) = @_; +return 0 if ! defined $buf || length($buf) = 0 || $buf =~ /\A\s*\z/m; +open my $out_fh, ' :encoding(utf-8)', $name or die output: $!; +$out_fh-print($buf); +close $out_fh or warn output: $!; +return 1; +} + Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' [1] Unwrapped, to prevent me breaking anything try to touch it up.. [2] Unless I forget again... diff -r 66d9870b3f36 -r e1df0178d049 data/split_batches.pl --- a/data/split_batches.pl Tue Feb 14 12:50:09 2012 -0500 +++ b/data/split_batches.pl Tue Feb 14 14:04:34 2012 -0500 @@ -40,10 +40,7 @@ { if(length($buf) + length($next_batch) $max_size) { -open my $out_fh, ' :encoding(utf-8)', -next_file($output, $i++) or die output: $!; -$out_fh-print($buf); -close $out_fh or warn output: $!; +$i++ if write_file(next_file($output, $i), $buf); $buf = ''; } $buf .= $next_batch; @@ -51,6 +48,9 @@ } } +$buf .= $next_batch; +write_file(next_file($output, $i++), $buf); + close $fh or warn input: $!; sub next_file @@ -85,3 +85,13 @@ return $size; } +sub write_file +{ +my ($name, $buf) = @_; +return 0 if ! defined $buf || length($buf) = 0 || $buf =~ /\A\s*\z/m; +open my $out_fh, ' :encoding(utf-8)', $name or die output: $!; +$out_fh-print($buf); +close $out_fh or warn output: $!; +return 1; +} + #!/usr/bin/perl use 5.010; use strict; use utf8; use warnings; use Data::Dumper; use IO::Handle; use Getopt::Long qw/GetOptionsFromArray HelpMessage :config auto_help bundling no_auto_abbrev no_getopt_compat no_ignore_case_always no_require_order permute/; my $batch_keyword = 'GO'; my $output = 'out.sql';
Re: Splitting and printing on a single line
bob wrote: Hello Hello, I am very new to perl and I need some guidance on the following issue My data set is like this --- Record 38 Richard Nixon http://en.wikipedia.org/wiki/Richard_Nixon --- Record 39 Gerald Ford http://en.wikipedia.org/wiki/Gerald_Ford --- Record 40 Jimmy Carter http://en.wikipedia.org/wiki/Jimmy_Carter and I would like to print it out like Record 38 \t Richard Nixon \t http://en.wikipedia.org/wiki/Richard_Nixon Record 39 \t Gerald Ford \t http://en.wikipedia.org/wiki/Gerald_Ford Record 40 \t Jimmy Carter \t http://en.wikipedia.org/wiki/Jimmy_Carter $ echo --- Record 38 Richard Nixon http://en.wikipedia.org/wiki/Richard_Nixon --- Record 39 Gerald Ford http://en.wikipedia.org/wiki/Gerald_Ford --- Record 40 Jimmy Carter http://en.wikipedia.org/wiki/Jimmy_Carter | perl -e' $/ = ( - x 27 ) . \n; $\ = \n; while ( ) { chomp; tr/\n/\t/; print; } ' Record 38 Richard Nixon http://en.wikipedia.org/wiki/Richard_Nixon Record 39 Gerald Ford http://en.wikipedia.org/wiki/Gerald_Ford Record 40 Jimmy Carterhttp://en.wikipedia.org/wiki/Jimmy_Carter John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Splitting URL into Patterns
Hi Akinleye, On Wed, 13 Jul 2011 16:53:35 +0100 AKINLEYE damola.akinl...@gmail.com wrote: please I need to split a bunch of URL into their respective domain name like abcd.com , it path direcory like /~bert/build/ , it's Argument constitutent like uid =1 Take for example msprogram.cn/update/ld.php?ld.phpid=1936rs=1765405346cc=0uid=1 I need the first match to be mspgroam.cn The Directory to be /update/ The File should be ld.php and argument should be a bunch of v=1 ,rs=1765405346 , cc=0 uid=1 For parsing URLs please see: http://beta.metacpan.org/release/URI Please do not do it using regular expressions. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Humanity - Parody of Modern Life - http://shlom.in/humanity Real programmers don’t write workarounds. They tell their users to upgrade their software. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Splitting URL into Patterns
On 7/13/11 Wed Jul 13, 2011 8:53 AM, AKINLEYE damola.akinl...@gmail.com scribbled: please I need to split a bunch of URL into their respective domain name like abcd.com , it path direcory like /~bert/build/ , it's Argument constitutent like uid =1 Take for example msprogram.cn/update/ld.php?ld.phpid=1936rs=1765405346cc=0uid=1 I need the first match to be mspgroam.cn The Directory to be /update/ The File should be ld.php and argument should be a bunch of v=1 ,rs=1765405346 , cc=0 uid=1 Parsing URLs and URIs is a common problem. When encountering a problem like this that other people may have encountered before, your first thought should be to search the CPAN repository at http://search.cpan.org for an appropriate module. For example, a few minutes searching on the terms URL and URI revealed the URI module, which may have exactly the functions you are looking for. Example: use URI; my $uri = URI-new('http://msprogram.cn/update/ld.php?uid=1'); print $uri-host() . \n; Outputs: msprogram.cn -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: splitting and replacing characters
On 11/9/09 Mon Nov 9, 2009 5:03 AM, rithu sundh...@gmail.com scribbled: Hi, Please help me to split a string as follows.. my $line = abcdefghijkl the expected output should be like: ab ef ij The logic is like alternate 2 characters should be removed split would not be the best tool for this task. I would use substr in an indexed for loop: my @output; for( my $i = 0; $i length($line); $i += 4 ) { push( @output, substr($line,$i,2) ); } However, here is a shortened form using regular expression: my @output = $line =~ m{ \G (..) .. }gx; Verify how either of these works when you do not have a multiple of 2 characters in your input. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: splitting and replacing characters
On Mon, Nov 9, 2009 at 7:03 AM, rithu sundh...@gmail.com wrote: Please help me to split a string as follows.. my $line = abcdefghijkl the expected output should be like: ab ef ij The logic is like alternate 2 characters should be removed Will a for loop work for your needs? The one below uses substr to extract every two characters. my $line=abcdefghijkl; for (my $i = 0; $i length( $line ); $i += 4) { print substr( $line, $i, 2 ), \n; } -- Robert Wohlfarth
Re: splitting and replacing characters
rithu wrote: Hi, Hello, Please help me to split a string as follows.. my $line = abcdefghijkl the expected output should be like: ab ef ij The logic is like alternate 2 characters should be removed $ perl -le' my $line = abcdefghijkl; my @data = unpack (a2x2)*, $line; print for @data; ' ab ef ij John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: splitting and replacing characters
On Mon, Nov 9, 2009 at 10:56 AM, Jim Gibson jimsgib...@gmail.com wrote: However, here is a shortened form using regular expression: my @output = $line =~ m{ \G (..) .. }gx; Verify how either of these works when you do not have a multiple of 2 characters in your input. It has other problems too: use strict; use warnings; use 5.010; my $str = ab--cd--ef; my @pieces = $str =~ /\G(..)../g; say @pieces; The output is: ab cd Note that the last two characters 'ef' were not included in the result. You need a multiple of 4 characters in the string for that regex to work correctly.
Re: splitting and replacing characters
On Mon, Nov 9, 2009 at 8:30 PM, tom smith climbingpartn...@gmail.comwrote: On Mon, Nov 9, 2009 at 10:56 AM, Jim Gibson jimsgib...@gmail.com wrote: However, here is a shortened form using regular expression: my @output = $line =~ m{ \G (..) .. }gx; Verify how either of these works when you do not have a multiple of 2 characters in your input. It has other problems too: use strict; use warnings; use 5.010; my $str = ab--cd--ef; my @pieces = $str =~ /\G(..)../g; say @pieces; The output is: ab cd Note that the last two characters 'ef' were not included in the result. You need a multiple of 4 characters in the string for that regex to work correctly. This works: use strict; use warnings; use 5.010; my $str = ab--cd--ef; my @pieces = $str =~ /\G(..)(?:..)?/gx; say @pieces; The output is: ab cd ef. The non-capturing parentheses (?:) allow you to apply a ? to the group, yet not have the match for that group inserted in the results list.
Re: Splitting two numbers joined by a minus sign
On Nov 9, 2:09 am, [EMAIL PROTECTED] (Rob Dixon) wrote: Why have you decided that the OP's solution doesn't provide what he wants, snipped it, and offered some code that does something very different? and he asked for a more elegant way of doing the same thing. I would have thought it was a good idea to assume he knew what he wanted. You're right. I didn't read his sample program, because I assumed that his subject also contained a description of what he wanted to do - split two numbers that were joined by a minus sign. How foolish of me to make such an assumption. The OP's subject does not match the OP's goal. Oh well. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Splitting two numbers joined by a minus sign
if the minus sign always there, then use s/// seems faster than split, like while(DATA){ s/(\d)-/$1\t-/; print $_; } On 11/9/07, Paul Lalli [EMAIL PROTECTED] wrote: On Nov 9, 2:09 am, [EMAIL PROTECTED] (Rob Dixon) wrote: Why have you decided that the OP's solution doesn't provide what he wants, snipped it, and offered some code that does something very different? and he asked for a more elegant way of doing the same thing. I would have thought it was a good idea to assume he knew what he wanted. You're right. I didn't read his sample program, because I assumed that his subject also contained a description of what he wanted to do - split two numbers that were joined by a minus sign. How foolish of me to make such an assumption. The OP's subject does not match the OP's goal. Oh well. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Splitting two numbers joined by a minus sign
michael wang wrote: if the minus sign always there, then use s/// seems faster than split, like while(DATA){ s/(\d)-/$1\t-/; print $_; } I'm pretty certain the OP wanted the data separated into fields so it would still have to be split. I do like the idea of adding the 'missing' space and then splitting though as it seems more intuitive, although I very much doubt if it is faster. Something like: while (DATA) { s/(?=\d)-/ -/g; my @data = split; print @data\n; } You can do some timings if you like! Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Splitting two numbers joined by a minus sign
On Nov 8, 3:32 pm, [EMAIL PROTECTED] (Demian) wrote: Hello, I ran into a problematic file that combined two numeric columns into one: ATOM325 CA GLU B 40 -30.254 72.432-297.620 1.00 10.00 C ATOM326 CA ASP B 41 -28.149 73.031-294.529 1.00 10.00 C ATOM327 CA GLU B 42 -27.716 76.690-295.429 1.00 10.00 C ATOM328 CA LEU B 43 -31.425 77.076-296.027 1.00 10.00 C ATOM329 CA VAL B 44 -32.237 75.542-292.673 1.00 10.00 C ATOM330 CA SER B 45 -29.850 77.900-290.914 1.00 10.00 C ATOM331 CA LEU B 46 -31.335 80.873-292.720 1.00 10.00 C ATOM332 CA GLN B 47 -34.837 79.809-291.801 1.00 10.00 C I came up with a solution, but I'm sure there's an easier way. Is there a more elegant way of doing it? Split on either whitespace or on a minus sign that is both followed by and preceded by a digit: my @fields = split /\s+|(?=\d)-(?=\d)/, $line; Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Splitting two numbers joined by a minus sign
On Nov 8, 2007 3:32 PM, Demian [EMAIL PROTECTED] wrote: Hello, I ran into a problematic file that combined two numeric columns into one: ATOM325 CA GLU B 40 -30.254 72.432-297.620 1.00 10.00 C ATOM326 CA ASP B 41 -28.149 73.031-294.529 1.00 10.00 C ATOM327 CA GLU B 42 -27.716 76.690-295.429 1.00 10.00 C ATOM328 CA LEU B 43 -31.425 77.076-296.027 1.00 10.00 C ATOM329 CA VAL B 44 -32.237 75.542-292.673 1.00 10.00 C ATOM330 CA SER B 45 -29.850 77.900-290.914 1.00 10.00 C ATOM331 CA LEU B 46 -31.335 80.873-292.720 1.00 10.00 C ATOM332 CA GLN B 47 -34.837 79.809-291.801 1.00 10.00 C I came up with a solution, but I'm sure there's an easier way. Is there a more elegant way of doing it? Thank you. _ #!/usr/bin/perl # use strict; use warnings; my $numstring= -100.00-400.34; my @pieces= split /([-])/, $numstring; my @nums; for (my $k=0; $k=$#pieces; $k++){ if ($pieces[$k] eq -) { push @nums, $pieces[$k]$pieces[$k+1]; $k++; next; } elsif($pieces[$k]=~ /\d+/) { push @nums, $pieces[$k]; } } map {print $_ . \n} @nums; __ [EMAIL PROTECTED]:~] $ cat b.pl #!/usr/bin/perl -w use strict; use Data::Dumper; my $numstring= -100.00-400.34; my @v = split /(\-?[0-9]+\.[0-9]+)?/,$numstring; print Dumper([EMAIL PROTECTED]); [EMAIL PROTECTED]:~] $ perl b.pl $VAR1 = [ '', '-100.00', '', '-400.34' ]; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- Rodrick R. Brown http://www.rodrickbrown.com
Re: Splitting two numbers joined by a minus sign
On Thursday 08 November 2007 17:48, Paul Lalli wrote: On Nov 8, 3:32 pm, [EMAIL PROTECTED] (Demian) wrote: I ran into a problematic file that combined two numeric columns into one: ATOM325 CA GLU B 40 -30.254 72.432-297.620 1.00 10.00 C ATOM326 CA ASP B 41 -28.149 73.031-294.529 1.00 10.00 C ATOM327 CA GLU B 42 -27.716 76.690-295.429 1.00 10.00 C ATOM328 CA LEU B 43 -31.425 77.076-296.027 1.00 10.00 C ATOM329 CA VAL B 44 -32.237 75.542-292.673 1.00 10.00 C ATOM330 CA SER B 45 -29.850 77.900-290.914 1.00 10.00 C ATOM331 CA LEU B 46 -31.335 80.873-292.720 1.00 10.00 C ATOM332 CA GLN B 47 -34.837 79.809-291.801 1.00 10.00 C I came up with a solution, but I'm sure there's an easier way. Is there a more elegant way of doing it? Split on either whitespace or on a minus sign that is both followed by and preceded by a digit: my @fields = split /\s+|(?=\d)-(?=\d)/, $line; Close but that matches and removes the leading negative sign. my @fields = split /\s+|(?=\d)(?=-\d)/, $line; Or you could do it like this: my @fields = map /-\d/ ? /-?\d+\.\d+/g : $_, split; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Splitting two numbers joined by a minus sign
Demian wrote: I ran into a problematic file that combined two numeric columns into one: ATOM325 CA GLU B 40 -30.254 72.432-297.620 1.00 10.00 C ATOM326 CA ASP B 41 -28.149 73.031-294.529 1.00 10.00 C ATOM327 CA GLU B 42 -27.716 76.690-295.429 1.00 10.00 C ATOM328 CA LEU B 43 -31.425 77.076-296.027 1.00 10.00 C ATOM329 CA VAL B 44 -32.237 75.542-292.673 1.00 10.00 C ATOM330 CA SER B 45 -29.850 77.900-290.914 1.00 10.00 C ATOM331 CA LEU B 46 -31.335 80.873-292.720 1.00 10.00 C ATOM332 CA GLN B 47 -34.837 79.809-291.801 1.00 10.00 C I came up with a solution, but I'm sure there's an easier way. Is there a more elegant way of doing it? Thank you. _ #!/usr/bin/perl # use strict; use warnings; my $numstring= -100.00-400.34; my @pieces= split /([-])/, $numstring; my @nums; for (my $k=0; $k=$#pieces; $k++){ if ($pieces[$k] eq -) { push @nums, $pieces[$k]$pieces[$k+1]; $k++; next; } elsif($pieces[$k]=~ /\d+/) { push @nums, $pieces[$k]; } } map {print $_ . \n} @nums; Hi Demian If I understand correctly you want to split each line of data on whitespace and also before all minus signs, whether or not they are preceded by whitespace. The program below achieves that requirement. I hope it helps you. Rob use strict; use warnings; while (DATA) { my @data = split /\s+|(?=-)/; print @data\n; } __DATA__ ATOM325 CA GLU B 40 -30.254 72.432-297.620 1.00 10.00 C ATOM326 CA ASP B 41 -28.149 73.031-294.529 1.00 10.00 C ATOM327 CA GLU B 42 -27.716 76.690-295.429 1.00 10.00 C ATOM328 CA LEU B 43 -31.425 77.076-296.027 1.00 10.00 C ATOM329 CA VAL B 44 -32.237 75.542-292.673 1.00 10.00 C ATOM330 CA SER B 45 -29.850 77.900-290.914 1.00 10.00 C ATOM331 CA LEU B 46 -31.335 80.873-292.720 1.00 10.00 C ATOM332 CA GLN B 47 -34.837 79.809-291.801 1.00 10.00 C **OUTPUT** ATOM 325 CA GLU B 40 -30.254 72.432 -297.620 1.00 10.00 C ATOM 326 CA ASP B 41 -28.149 73.031 -294.529 1.00 10.00 C ATOM 327 CA GLU B 42 -27.716 76.690 -295.429 1.00 10.00 C ATOM 328 CA LEU B 43 -31.425 77.076 -296.027 1.00 10.00 C ATOM 329 CA VAL B 44 -32.237 75.542 -292.673 1.00 10.00 C ATOM 330 CA SER B 45 -29.850 77.900 -290.914 1.00 10.00 C ATOM 331 CA LEU B 46 -31.335 80.873 -292.720 1.00 10.00 C ATOM 332 CA GLN B 47 -34.837 79.809 -291.801 1.00 10.00 C -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Splitting two numbers joined by a minus sign
Paul Lalli wrote: On Nov 8, 3:32 pm, [EMAIL PROTECTED] (Demian) wrote: Hello, I ran into a problematic file that combined two numeric columns into one: ATOM325 CA GLU B 40 -30.254 72.432-297.620 1.00 10.00 C ATOM326 CA ASP B 41 -28.149 73.031-294.529 1.00 10.00 C ATOM327 CA GLU B 42 -27.716 76.690-295.429 1.00 10.00 C ATOM328 CA LEU B 43 -31.425 77.076-296.027 1.00 10.00 C ATOM329 CA VAL B 44 -32.237 75.542-292.673 1.00 10.00 C ATOM330 CA SER B 45 -29.850 77.900-290.914 1.00 10.00 C ATOM331 CA LEU B 46 -31.335 80.873-292.720 1.00 10.00 C ATOM332 CA GLN B 47 -34.837 79.809-291.801 1.00 10.00 C I came up with a solution, but I'm sure there's an easier way. Is there a more elegant way of doing it? Split on either whitespace or on a minus sign that is both followed by and preceded by a digit: my @fields = split /\s+|(?=\d)-(?=\d)/, $line; Why have you decided that the OP's solution doesn't provide what he wants, snipped it, and offered some code that does something very different? Demian posted this program: use strict; use warnings; my $numstring= -100.00-400.34; my @pieces= split /([-])/, $numstring; my @nums; for (my $k=0; $k=$#pieces; $k++){ if ($pieces[$k] eq -) { push @nums, $pieces[$k]$pieces[$k+1]; $k++; next; } elsif($pieces[$k]=~ /\d+/) { push @nums, $pieces[$k]; } } map {print $_ . \n} @nums; __END__ which outputs -100.00 -400.34 and he asked for a more elegant way of doing the same thing. I would have thought it was a good idea to assume he knew what he wanted. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Splitting two numbers joined by a minus sign
On Nov 8, 3:32 pm, [EMAIL PROTECTED] (Demian) wrote: Hello, I ran into a problematic file that combined two numeric columns into one: ATOM325 CA GLU B 40 -30.254 72.432-297.620 1.00 10.00 C ATOM326 CA ASP B 41 -28.149 73.031-294.529 1.00 10.00 C ATOM327 CA GLU B 42 -27.716 76.690-295.429 1.00 10.00 C ATOM328 CA LEU B 43 -31.425 77.076-296.027 1.00 10.00 C ATOM329 CA VAL B 44 -32.237 75.542-292.673 1.00 10.00 C ATOM330 CA SER B 45 -29.850 77.900-290.914 1.00 10.00 C ATOM331 CA LEU B 46 -31.335 80.873-292.720 1.00 10.00 C ATOM332 CA GLN B 47 -34.837 79.809-291.801 1.00 10.00 C I came up with a solution, but I'm sure there's an easier way. Is there a more elegant way of doing it? Thank you. _ #!/usr/bin/perl # use strict; use warnings; my $numstring= -100.00-400.34; my @pieces= split /([-])/, $numstring; my @nums; for (my $k=0; $k=$#pieces; $k++){ if ($pieces[$k] eq -) { push @nums, $pieces[$k]$pieces[$k+1]; $k++; next; } elsif($pieces[$k]=~ /\d+/) { push @nums, $pieces[$k]; } } map {print $_ . \n} @nums; __ Based on your example, I'm not sure what you final goal is, as the string you used does not look like a line from your sample data. I came up with an example using your data which may or may not be what you want. use warnings; use strict; while (DATA) { # This should fix the line if the minus sign is always # preceded and followed by a digit s{(\d)(-\d)} {$1 $2}g; # if for some reason you only want numbers my @pieces= split ( ); my @nums; foreach my $piece ( @pieces ) { # There is probably a better way to do this. # This is a fairly simple regular expression # that does not handle the case where a number # might end with a period. if ( $piece =~ /^-?\d+(?:.\d+)?$/ ) { push @nums, $piece; } } map {print $_ . \n} @nums; } __DATA__ ATOM325 CA GLU B 40 -30.254 72.432-297.620 1.00 10.00 C ATOM326 CA ASP B 41 -28.149 73.031-294.529 1.00 10.00 C ATOM327 CA GLU B 42 -27.716 76.690-295.429 1.00 10.00 C ATOM328 CA LEU B 43 -31.425 77.076-296.027 1.00 10.00 C ATOM329 CA VAL B 44 -32.237 75.542-292.673 1.00 10.00 C ATOM330 CA SER B 45 -29.850 77.900-290.914 1.00 10.00 C ATOM331 CA LEU B 46 -31.335 80.873-292.720 1.00 10.00 C ATOM332 CA GLN B 47 -34.837 79.809-291.801 1.00 10.00 C __END__ Of course there are many ways to do this. HTH, Ken -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Splitting a CSV file at a variable number
On 6/28/07, sum_duud [EMAIL PROTECTED] wrote: in essence I would like the perl script to output all the fourth column 0 values to a file called phase_0.csv and all the 1 values to phase_1.csv etc. snip use an array of file handles (warning, untested): use strict; use warnings; open my $in, '', phase.csv or die Could not open phase.csv: $!\n; $in; #discard first line my @file = map { open my $fh, '', phase_$_.csv or die could not open phase_$_.csv: $!; $fh } 0 .. 20; while ($in) { chomp; my ($x, $y, $z, $phase) =split /\s*,\s*/; my $file = $file[$phase]; print $file $x, $y, $z, $phase\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: splitting the line correctly
A Diumenge 10 Setembre 2006 01:56, Robert Krueger va escriure: Hi, I bought 3 books a few days ago on Perl, and things are going well with one exception. An example: @line = this is a very long line word1+word2+word3 and it contines on and on; The object is to execute a split using + so I end up with word1, word2, and word3, so I do this: my @line = split('\+'); According to the books, I should end up with this: $line[0] = word1 $line[1] = word2 $line[3] = word3 Instead it ends up to be this: $line[0] = this is a very long line word1 $line[1] = word2 $line[3] = word3 and it contines on and on I can't figure out what I'm doing wrong. Can someone explain? Thanks, Robert Syntax of split function is: @fields = split /separator/, $string; (using default split is: @fields = split;). Using 'separator' you're taking a literal as a separator. -- Xavier Mas -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting the line correctly
Robert Krueger wrote: Hi, I bought 3 books a few days ago on Perl, and things are going well with one exception. An example: @line = this is a very long line word1+word2+word3 and it contines on and on; The object is to execute a split using + so I end up with word1, word2, and word3, so I do this: my @line = split('\+'); According to the books, I should end up with this: $line[0] = word1 $line[1] = word2 $line[3] = word3 Instead it ends up to be this: $line[0] = this is a very long line word1 $line[1] = word2 $line[3] = word3 and it contines on and on I can't figure out what I'm doing wrong. Can someone explain? Hello Robert You're misreading or misunderstanding your book I'm afraid. split() will simply split its second parameter (which is $_ by default if you don't specify one) into chunks at places that match its first parameter (which is actually a regular expression); nothing cleverer than that. So you can see that it has done its job correctly. By the way you need a dollar sign instead of an at on that line $line = this is a very long line ...; It's hard to see what the author could have intended, as its awkward to get that result from the string you've shown in a single step. Perhaps something like this will help you see where you're going wrong. It first splits the string at each section of whitespace, which is what split() does by default, and then takes the seventh item (the string 'word1+word2+word3') and splits that at the plus signs, giving you the result you expected. This is probably nothing like the book's author intended, but may lead you back onto the right track. Good luck. Rob use strict; use warnings; $_ = this is a very long line word1+word2+word3 and it contines on and on; my @line = split; my @words = split /\+/, $line[6]; print $_\n foreach @words; *OUTPUT* word1 word2 word3 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting the line correctly
Robert Krueger wrote: Hi, Hello, I bought 3 books a few days ago on Perl, and things are going well with one exception. An example: @line = this is a very long line word1+word2+word3 and it contines on and on; The object is to execute a split using + so I end up with word1, word2, and word3, so I do this: my @line = split('\+'); According to the books, I should end up with this: $line[0] = word1 $line[1] = word2 $line[3] = word3 Instead it ends up to be this: $line[0] = this is a very long line word1 $line[1] = word2 $line[3] = word3 and it contines on and on I can't figure out what I'm doing wrong. Can someone explain? You are not doing anything wrong, that is how split() works, everything upto the first '+' is put into $line[0] and everything between the first and second '+' is put into $line[1] and everything between the second and the third '+' is put into $line[3], etc., etc. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting strings
On Aug 30, 2006, at 3:42 AM, Dr.Ruud wrote: Aaargh, I was suddenly mixing up split /()/ and /()/g. I really shouldn't post anymore without testing. Thank you all for the clarifications regarding split(). I should pay more attention when I read the documentation (or get more sleep). -Hien. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting strings
Hien Le schreef: my @bar2 = split( /([a-z]{5})/, $foo );# Captures white-spaces ?!? Because of the capturing (), split also returns the separators. See perldoc -f split. Suggestion: my @bar2 = split( /./, $foo ); -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting strings
Hien Le wrote: Hello, Hello, Given the string 'abcdefghijklmnopq', I wish to add a line break every 5 characters: abcde fghij klmno pq $ perl -e' my $foo = q[abcdefghijklmnopq]; print $foo\n; $foo =~ s/(.{0,5})/$1\n/g; print $foo; ' abcdefghijklmnopq abcde fghij klmno pq Method 1 below works, but my split() in method 2 captures 'something unexpected' at each match. Could someone please tell me what is split capturing that I am not seeing? split( /X/, 'aXb' ) splits the string using the pattern and returns the list ( 'a', 'b' ). split( /(X)/, 'aXb' ) splits the string using the pattern and returns the list ( 'a', 'X', 'b' ). Everything not in the pattern is returned in the list unless you use capturing parentheses and then everything in the capturing parentheses is returned as well. My script: == #!/usr/bin/perl -w use strict; my $foo = 'abcdefghijklmnopq'; # Method 1 print( \nMethod 1\n ); my $foo_length = length( $foo ); for( my $i = 0; $i $foo_length; $i += 5 ) { my $bar1 = substr( $foo, $i, 5 ); print( $bar1, \n ); } # Method 2 print( \nMethod 2\n ); my @bar2 = split( /([a-z]{5})/, $foo );# Captures white-spaces ?!? my $bar2_nb = @bar2; print( join( \n, @bar2) ); print( \nElements in array = , $bar2_nb, \n ); # 7 elements in the array. __END__ $ perl -e' my $foo = q[abcdefghijklmnopq]; print $foo\n; my @bar = unpack q[(a5)*], $foo; print map $_\n, @bar; ' abcdefghijklmnopq abcde fghij klmno pq $ perl -e' my $foo = q[abcdefghijklmnopq]; print $foo\n; my @bar = $foo =~ /.{0,5}/g; print map $_\n, @bar; ' abcdefghijklmnopq abcde fghij klmno pq John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting strings
On 08/29/2006 06:52 AM, Hien Le wrote: [...] # Method 2 print( \nMethod 2\n ); my @bar2 = split( /([a-z]{5})/, $foo );# Captures white-spaces ?!? [...] The comments made by Dr. Ruud and John W. Krahn are correct. Split is returning the empty strings between delimiter segments in the original string. To zap these out, do this: my @bar2 = grep length, split (/([a-z]{5})/, $foo); Any substrings with a length of zero will be removed by grep length. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting strings
Mumia W. schreef: Hien Le: [...] # Method 2 print( \nMethod 2\n ); my @bar2 = split( /([a-z]{5})/, $foo );# Captures white-spaces ?!? [...] The comments made by Dr. Ruud and John W. Krahn are correct. Split is returning the empty strings between delimiter segments in the original string. To zap these out, do this: my @bar2 = grep length, split (/([a-z]{5})/, $foo); Any substrings with a length of zero will be removed by grep length. Huh? Why not just remove the capturing ()? Again: perldoc -f split -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting strings
On 08/29/2006 05:02 PM, Dr.Ruud wrote: Mumia W. schreef: Hien Le: [...] # Method 2 print( \nMethod 2\n ); my @bar2 = split( /([a-z]{5})/, $foo );# Captures white-spaces ?!? [...] The comments made by Dr. Ruud and John W. Krahn are correct. Split is returning the empty strings between delimiter segments in the original string. To zap these out, do this: my @bar2 = grep length, split (/([a-z]{5})/, $foo); Any substrings with a length of zero will be removed by grep length. Huh? Why not just remove the capturing ()? Again: perldoc -f split Without the capturing parentheses, split will remove every sequence of five alphabetic characters from the output. Only 'pq' will remain: use Data::Dumper; my $foo = 'abcdefghijklmnopq'; my @foo = split /[a-z]{5}/, $foo; print Dumper([EMAIL PROTECTED]); __END__ That program prints this: $VAR1 = [ '', '', '', 'pq' ]; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting strings
Mumia W. schreef: Dr.Ruud: Mumia W.: my @bar2 = grep length, split (/([a-z]{5})/, $foo); Any substrings with a length of zero will be removed by grep length. Huh? Why not just remove the capturing ()? Without the capturing parentheses, split will remove every sequence of five alphabetic characters from the output. Only 'pq' will remain: use Data::Dumper; my $foo = 'abcdefghijklmnopq'; my @foo = split /[a-z]{5}/, $foo; print Dumper([EMAIL PROTECTED]); __END__ That program prints this: $VAR1 = [ '', '', '', 'pq' ]; Aaargh, I was suddenly mixing up split /()/ and /()/g. I really shouldn't post anymore without testing. #!/usr/bin/perl use warnings ; use strict ; use Data::Dumper ; my $foo = 'abcdefghijklmnopq' ; my @foo = ($foo =~ /([a-z]{1,5})/g) ; print Dumper([EMAIL PROTECTED]); __END__ $VAR1 = [ 'abcde', 'fghij', 'klmno', 'pq' ]; -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting a script into multiple files
Check out 'perldoc -f do'. Also, get a new laptop. :) -Original Message- From: Martin Tournoij [mailto:[EMAIL PROTECTED] Sent: Wednesday, July 26, 2006 10:05 AM To: beginners@perl.org Subject: Splitting a script into multiple files snip My laptop, on which I do all my Programming/Developing/Writing, is quite old, and also quite slow, and vim is getting slower with every line I add Is there some way to easily split my project up into several files? something like php include()/require()? snip -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting a script into multiple files
Great, thanks for the fast reply. I kindly accept all donations for a new laptop On 26/07/06, Timothy Johnson [EMAIL PROTECTED] wrote: Check out 'perldoc -f do'. Also, get a new laptop. :) -Original Message- From: Martin Tournoij [mailto:[EMAIL PROTECTED] Sent: Wednesday, July 26, 2006 10:05 AM To: beginners@perl.org Subject: Splitting a script into multiple files snip My laptop, on which I do all my Programming/Developing/Writing, is quite old, and also quite slow, and vim is getting slower with every line I add Is there some way to easily split my project up into several files? something like php include()/require()? snip -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting on every other delimiter
On 5/1/06, Rance Hall [EMAIL PROTECTED] wrote: assume the following code: my $output = qx{systemcall}; assume that the output of the systemcall looks something like this name\n value\n snip what like to do is get the name/value pairs into a hash of some sort snip Why are you using a scalar? This is the perfect place to use an array: my @output = qx(systemcall}; chomp(@output); my %hash = @output; #first val becomes key, second val, third key, fourth, val, etc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting on every other delimiter @ 1146493312
Intrah onat Diria .. Mon, 01 May 2006 09:06:21 -0500 , Rance Hall wrote Revera y: http://learn.perl.org/ http://learn.perl.org/first-response For additional commands, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] -- [EMAIL PROTECTED] 1-308-468-5711, ext. 106 Nebraska Turkey Growers System Administrator Rance Hall -- Thanks. But I'm open to suggestions about HOW to accomplish this. split each name\nvalue pair. I think what I need to do is split every other \n and then go back and split wants to split on EACH \n what like to do is get the name/value pairs into a hash of some sort my %hash = grep s/\n//, @$output; , assuming that $output is a reference to an @rray ... value\n name\n value\n name\n value\n name\n value\n name\n value\n name\n value\n name\n assume that the output of the systemcall looks something like this my $output = qx{systemcall}; assume the following code: the following could be unreadable @ 1146493586 ::: | , 28-2-2006-23:24 , ],in,andand], , , , 134 , DELETED , , terrfastefaster , for , / , , | , -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting on every other delimiter
Chas Owens wrote: On 5/1/06, Rance Hall [EMAIL PROTECTED] wrote: assume the following code: my $output = qx{systemcall}; assume that the output of the systemcall looks something like this name\n value\n snip what like to do is get the name/value pairs into a hash of some sort snip Why are you using a scalar? This is the perfect place to use an array: my @output = qx(systemcall}; chomp(@output); my %hash = @output; #first val becomes key, second val, third key, fourth, val, etc. What a great question: Why was I using a scalar? Two points: 1. I never found any documentation that suggested such a thing was possible. 2. Being new at this more often than not my errors are syntax related. I'd like to get that out of the way so that when I try something and it doesn't work, the reason is obvious, right now it isn't and I'm spending alot of time checking things that are correct in order to find the things that aren't. Thanks for the hint, that will help quite a bit. -- Rance Hall System Administrator Nebraska Turkey Growers 1-308-468-5711, ext. 106 [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting on every other delimiter
Rance Hall schreef: name1\n value1\n name2\n value2\n ... what like to do is get the name/value pairs into a hash of some sort There just was a thread on this on news:comp.language.perl.misc , see news:[EMAIL PROTECTED] and on. -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting on every other delimiter
Rance Hall wrote: assume the following code: my $output = qx{systemcall}; assume that the output of the systemcall looks something like this name\n value\n name\n value\n name\n value\n name\n value\n name\n value\n name\n value\n ... what like to do is get the name/value pairs into a hash of some sort split wants to split on EACH \n I think what I need to do is split every other \n and then go back and split each name\nvalue pair. But I'm open to suggestions about HOW to accomplish this. In list context qx// returns a list and you can assign a list directly to a hash, for example: my %hash = qw{ one two three four }; So the only remaining problem is ensuring that the list contains an even number of items. If you assume that your data is valid then a simple way to do what you want would be: my %hash = qx{systemcall} =~ /.+/g; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting text that contains an encrypted value
On Fri, 2006-28-04 at 14:16 -0500, Rance Hall wrote: here is the prelim setup: my $delimiter = my $teststring = name;;;encryptedpassword;;;date; my @userdetails = split($delimiter, $teststring); here is the goal: I would like to find a delimiter value that I can use to both create and read from a flat-file database where splitting on the $delimiter doesn't improperly split either the encryptedpassword value, or a reasonable date format. Question: What would a good value for $delimiter be? The only way to guarantee that your delimiter won't be part of the data is to make it longer than any possible field. That way, it would be impossible for your encryption algorithm to accidentally create it. -- __END__ Just my 0.0002 million dollars worth, --- Shawn For the things we have to learn before we can do them, we learn by doing them. Aristotle * Perl tutorials at http://perlmonks.org/?node=Tutorials * A searchable perldoc is at http://perldoc.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting text that contains an encrypted value
Rance Hall wrote: here is the prelim setup: my $delimiter = my $teststring = name;;;encryptedpassword;;;date; my @userdetails = split($delimiter, $teststring); here is the goal: I would like to find a delimiter value that I can use to both create and read from a flat-file database where splitting on the $delimiter doesn't improperly split either the encryptedpassword value, or a reasonable date format. Question: What would a good value for $delimiter be? Ive got an idea that my three semicolon approach is not going to stand up to all possible password encryption schemes, so I probably need something more bullet proof than this. Three things that may work: 1. Convert the password field to a character set that does not conflict with the delimiter (like passwd(5) does.) 2. Put the password field at the end of the line. For example: my $teststring = 'name;date;encryptedpassword'; my @userdetails = split /$delimiter/, $teststring, 3; 3. Use fixed length fields. For example: my $teststring = pack 'a20 a30 a8', 'name', 'encryptedpassword', 'date'; my @userdetails = unpack 'a20 a30 a8', $teststring; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting on white space
It isn't splitting on the 'two three' because it doesn't match. / \s{1,}/ matches a space followed by one or more whitespace characters (you have a space in front of your \s) -Original Message- From: John Bruin [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 15, 2006 5:37 PM To: beginners@perl.org Subject: Splitting on white space Can anyone please tell me why the code below is not splitting the 'two three'? snip my $string = 'one two threefour'; my @temp = split(/ \s{1,}/,$string); snip -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting on white space
On 3/15/06, John Bruin [EMAIL PROTECTED] wrote: my $string = 'one two threefour'; my @temp = split(/ \s{1,}/,$string); Your pattern matches a space followed by one or more whitespace characters (such as tab, space, or newline). There's just a single space between two and three, and the pattern needs to match at least two characters, so the pattern can't match there. Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting on white space
Thanks very much - I has completely missed whitespace before the slash. John -Original Message- From: Timothy Johnson [mailto:[EMAIL PROTECTED] Sent: 16 March 2006 14:41 To: John Bruin; beginners@perl.org Subject: RE: Splitting on white space It isn't splitting on the 'two three' because it doesn't match. / \s{1,}/ matches a space followed by one or more whitespace characters (you have a space in front of your \s) -Original Message- From: John Bruin [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 15, 2006 5:37 PM To: beginners@perl.org Subject: Splitting on white space Can anyone please tell me why the code below is not splitting the 'two three'? snip my $string = 'one two threefour'; my @temp = split(/ \s{1,}/,$string); snip -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting file
-Original Message- From: Raoul Ripmeester [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 10, 2006 4:48 PM To: beginners@perl.org Subject: Splitting file Hello all, Hello, I am a newbe so please bare with me : I am trying to split lines of a file into variables so I can do some sorting and other stuff with this. Now my script is working but the split is giving me problems. I am splitting on the space character, but the line(s) can contain multiple spaces. Underneath a few sample lines that I am trying to split. logfile 7-NOV-2005 09:24:00.56 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:24:24.36 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:25:16.44 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:25:42.22 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:27:53.48 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:29:51.32 LOGOUT LOCALSMC8 SMCMAN 02DF _TXA4: 7-NOV-2005 09:46:13.94 LOGIN LOCALSMC8 SMCMAN 02E0 _TXA4: /logfile And here is a part of the code I am using ( the print statements are just to test the splitting ) : code open (FILE, $file ) || die (open of $file failed $! \n) ; while (FILE){ To match one or more spaces, your regex should be: /\s+/ This: ($date,$time,$type,$subtype,$node,$username,$ID, $term) = split(/ /,$_) ; becomes: ($date,$time,$type,$subtype,$node,$username,$ID, $term) = split(/\s+/,$_) ; ry print date = $date \t time = $time \t type = $type \n ; print subtype = $subtype \t node = $ node \t username = $username \n ; print ID = $ID \t term = $term \n ; } /code How can I make the split to work with one or multiple space. I tried to split on / .*/ But that will put the whole line in the first variable, so what am I missing. Thanks and best regards, Raoul. -- Censorship cannot eliminate evil, it can only kill freedom. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting file
On Tue, Jan 10, 2006 at 10:47:50PM +0100 or thereabouts, Raoul Ripmeester wrote: Hello all, I am a newbe so please bare with me : I am trying to split lines of a file into variables so I can do some sorting and other stuff with this. Now my script is working but the split is giving me problems. I am splitting on the space character, but the line(s) can contain multiple spaces. Underneath a few sample lines that I am trying to split. logfile 7-NOV-2005 09:24:00.56 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:24:24.36 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:25:16.44 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:25:42.22 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:27:53.48 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:29:51.32 LOGOUT LOCALSMC8 SMCMAN 02DF _TXA4: 7-NOV-2005 09:46:13.94 LOGIN LOCALSMC8 SMCMAN 02E0 _TXA4: /logfile This looks like fixed width data to me. And here is a part of the code I am using ( the print statements are just to test the splitting ) : code open (FILE, $file ) || die (open of $file failed $! \n) ; while (FILE){ ($date,$time,$type,$subtype,$node,$username,$ID, $term) = split(/ /,$_) ; print date = $date \t time = $time \t type = $type \n ; print subtype = $subtype \t node = $ node \t username = $username \n ; print ID = $ID \t term = $term \n ; } /code How can I make the split to work with one or multiple space. I tried to split on / .*/ But that will put the whole line in the first variable, so what am I missing. I had to do something like this a while back ... had the same problem. A friend of mine introduced me to a different function -- unpack. Unfortuneately . I don't remember the exact syntax. Thanks and best regards, Raoul. -- Censorship cannot eliminate evil, it can only kill freedom. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- Casey Cichon [EMAIL PROTECTED] Never ascribe to malice that which could merely be incompetence. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting file
Ryan, Yes thank you that works and did the trick. Kind regards, Raoul. On Tue, 2006-01-10 at 16:48 -0500, Ryan Frantz wrote: -Original Message- From: Raoul Ripmeester [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 10, 2006 4:48 PM To: beginners@perl.org Subject: Splitting file Hello all, Hello, I am a newbe so please bare with me : I am trying to split lines of a file into variables so I can do some sorting and other stuff with this. Now my script is working but the split is giving me problems. I am splitting on the space character, but the line(s) can contain multiple spaces. Underneath a few sample lines that I am trying to split. logfile 7-NOV-2005 09:24:00.56 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:24:24.36 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:25:16.44 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:25:42.22 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:27:53.48 PRIVILEGE PRVAUD_SUCCESS SMC8 SMCMAN 02DF TXA4: 7-NOV-2005 09:29:51.32 LOGOUT LOCALSMC8 SMCMAN 02DF _TXA4: 7-NOV-2005 09:46:13.94 LOGIN LOCALSMC8 SMCMAN 02E0 _TXA4: /logfile And here is a part of the code I am using ( the print statements are just to test the splitting ) : code open (FILE, $file ) || die (open of $file failed $! \n) ; while (FILE){ To match one or more spaces, your regex should be: /\s+/ This: ($date,$time,$type,$subtype,$node,$username,$ID, $term) = split(/ /,$_) ; becomes: ($date,$time,$type,$subtype,$node,$username,$ID, $term) = split(/\s+/,$_) ; ry print date = $date \t time = $time \t type = $type \n ; print subtype = $subtype \t node = $ node \t username = $username \n ; print ID = $ID \t term = $term \n ; } /code How can I make the split to work with one or multiple space. I tried to split on / .*/ But that will put the whole line in the first variable, so what am I missing. Thanks and best regards, Raoul. -- Censorship cannot eliminate evil, it can only kill freedom. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- Censorship cannot eliminate evil, it can only kill freedom. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting file
Raoul Ripmeester schreef: How can I make the split to work with one or multiple space. What you aim for, is already the default behaviour of split. perldoc -f split If PATTERN is also omitted, splits on whitespace (after skipping any leading whitespace). [...] As a special case, specifying a PATTERN of space (' ') will split on white space just as split with no arguments does. So change your split(/ /,$_) into split ' ' or even into just split -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting on =
Tommy Nordgren wrote: How do you split a string on the equality character? Neither split (/=/,$myvar) nor split ('=',$myvar) appears to work How does it not work? It works for me. $ perl -le' print for split /=/, one=two ' one two John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting on =
Tommy Nordgren wrote: How do you split a string on the equality character? Neither split (/=/,$myvar) nor split ('=',$myvar) appears to work Home is not where you are born, but where your heart finds peace - Tommy Nordgren, The dying old crone Really need the context of what you were attempting. Showing what you have in $_. The first split /=/, should work, but really need to see what you were attempting. Wags ;) *** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. *** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting on =
Sep 21, 2005 kl. 9:54 PM skrev John W. Krahn: Tommy Nordgren wrote: How do you split a string on the equality character? Neither split (/=/,$myvar) nor split ('=',$myvar) appears to work How does it not work? When called with the syntax I've used, it returns a list containing a single string. I'll try without the paranthesis. Can it be that perl treats the entire source text between left and right paranthesis as a regular expression. If so, I consider this a bug. It works for me. $ perl -le' print for split /=/, one=two ' one two John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response Home is not where you are born, but where your heart finds peace - Tommy Nordgren, The dying old crone -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting on =
On Sep 21, Tommy Nordgren said: When called with the syntax I've used, it returns a list containing a single string. I'll try without the paranthesis. Can it be that perl treats the entire source text between left and right paranthesis as a regular expression. If so, I consider this a bug. There's no bug. You're doing something wrong. If you show us a bit more code, that demonstrates how you're using the function, we might be able to help more. -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting on =
Tommy Nordgren wrote: How do you split a string on the equality character? Neither split (/=/,$myvar) nor split ('=',$myvar) appears to work Home is not where you are born, but where your heart finds peace - Tommy Nordgren, The dying old crone You are doing something incorrectly. Please post more code. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting on =
Tommy Nordgren wrote: Sep 21, 2005 kl. 9:54 PM skrev John W. Krahn: Tommy Nordgren wrote: How do you split a string on the equality character? Neither split (/=/,$myvar) nor split ('=',$myvar) appears to work How does it not work? When called with the syntax I've used, it returns a list containing a single string. Maybe $myvar contains no data after the '=' character? I'll try without the paranthesis. Can it be that perl treats the entire source text between left and right paranthesis as a regular expression. No, it treats the first argument (before the comma) as a regular expression (with or without parentheses.) John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting on =
Sep 21, 2005 kl. 9:55 PM skrev Wagner, David --- Senior Programmer Analyst --- WGO: Tommy Nordgren wrote: How do you split a string on the equality character? Neither split (/=/,$myvar) nor split ('=',$myvar) appears to work Home is not where you are born, but where your heart finds peace - Tommy Nordgren, The dying old crone Really need the context of what you were attempting. Showing what you have in $_. The first split /=/, should work, but really need to see what you were attempting. Wags ;) This is my full script: #!/usr/bin/perl use strict; use warnings; if ( @ARGV !=2) { die Usage: nibfixup Nibfile Applicationname\n ; } my $nibfile = $ARGV[0]; my $appname = $ARGV[1]; print Running nibtool\n; my $dictionary = `/usr/bin/nibtool -L $nibfile`; print Nibtool have run\n; my @dictionary = split(/\n/,$dictionary); my $len = @dictionary; print lenght:$len\n; open (DICT,'','temp.strings') or die Could not open dictionary file \n; for my $i (@dictionary) { my @i = split /=/ , $i; if (1 == length @i) { print DICT $i,\n; } else { my $j = shift @i; print DICT $j=; $j = shift @i; $j =~ s/NewApplication/$appname/e; print DICT $j; for $j (@i) { print DICT =$j; } print DICT \n; } } close DICT; system ('/bin/mv' ,$nibfile ,old.$nibfile) and die Could not rename nib file\n; print Hellow world!\n; print Running nibtool\n; system ('/usr/bin/nibtool' ,'-w',$nibfile,'-d','temp.strings',old. $nibfile) and die Could not update nibfile\n; print Nibtool have run\n; system ('/bin/rm', '-rf', old.$nibfile); For some reason, I don't get $i split on = in the lines where it occurs, I'm trying to write a temp.strings file, where the second, past equalty sign, occurence of the word NewApplication,(localized) Gets replaced by a string passed as second parameter on the command line - This sig is dedicated to the advancement of Nuclear Power Tommy Nordgren [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting on =
Tommy Nordgren wrote: This is my full script: #!/usr/bin/perl use strict; use warnings; if ( @ARGV !=2) { die Usage: nibfixup Nibfile Applicationname\n ; } my $nibfile = $ARGV[0]; my $appname = $ARGV[1]; print Running nibtool\n; my $dictionary = `/usr/bin/nibtool -L $nibfile`; print Nibtool have run\n; my @dictionary = split(/\n/,$dictionary); my $len = @dictionary; print lenght:$len\n; open (DICT,'','temp.strings') or die Could not open dictionary file \n; for my $i (@dictionary) { my @i = split /=/ , $i; if (1 == length @i) { length() tests the length of *strings* NOT *arrays*. @i in scalar context is the number of elements in @i so if: @i = ( 'one', 'two' ); then: 2 == scalar @i; So you are saying something like: if (1 == length '2') { So your if statement will be true if @i contains zero to nine elements and false if it contains ten or more elements. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting on =
Sep 21, 2005 kl. 9:48 PM skrev Tommy Nordgren: How do you split a string on the equality character? Neither split (/=/,$myvar) nor split ('=',$myvar) appears to work Home is not where you are born, but where your heart finds peace - Tommy Nordgren, The dying old crone -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response I've verified that the problem with my script occurs because the string I'm trying to parse is using a different coding of the equality sign. The default output format of the tool whose output I'm trying to process is UniCode . Apparently there is multiple code point for the = character in unicode. telling the tool whose output I'm trying to process to use the Mac Roman encoding instead, succeds. How can you properly match = characters in unicode strings? - This sig is dedicated to the advancement of Nuclear Power Tommy Nordgren [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: splitting data stream
From: Octavian Rasnita [mailto:[EMAIL PROTECTED] Sent: Thursday, August 18, 2005 8:04 AM To: beginners perl Subject: splitting data stream Hi, I want to use regular expressions (or other techniques) to split a data stream which has the following format: size_of_next_bodybodysize_of_next_bodybody... The size_of_next_body is a 4 digit number and tells the size of the next body it should get. Thank you very much. Teddy It's not clear from your note exactly what the data stream looks like... Is it delimited between fields? Are the fields in a record and is the record delimited? Can you dump some of the data and include it in a post? But given the above... perl -e' $a=q{0010testtest1X0005testX0001A0003BBX0006CX}; while (length($a)) { $size=substr($a,0,4); $body[$i]=substr($a,4,$size); $a=substr($a,4+$size); $i++ } print $_\n foreach (@body); ' DEV,SUN2 testtest1X testX A BBX CX DEV,SUN2 jwm -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting data stream
On Aug 18, Octavian Rasnita said: I want to use regular expressions (or other techniques) to split a data stream which has the following format: size_of_next_bodybodysize_of_next_bodybody... The size_of_next_body is a 4 digit number and tells the size of the next body it should get. You can roll your own with substr() and what-not, or you can use the unpack() function to do it for you: my @chunks = unpack (A4/A)*, $buffer; The A4/A means read four characters and get that many characters; the (...)* around that means do this repeatedly. -- Jeff japhy Pinyan % How can we ever be the sold short or RPI Acacia Brother #734 % the cheated, we who for every service http://japhy.perlmonk.org/ % have long ago been overpaid? http://www.perlmonks.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting based on index
Hi Could you describe a bit more what you are trying to accomplish? Gretar Mar Saurabh Singhvi wrote: hi all if (($pos = index($free,M,$pos)) -1) { print Found at $pos\n; } now what i want to do is except the part that was found in index should be outputted to another variable as an integer ...how should i do that?? thnx in adv rgds Saurabh -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting by number
On 17 Feb 2005, at 21:40, Doug Essinger-Hileman wrote: On 17 Feb 2005 at 11:27, Ken Gillett wrote: Initially the storage requirements are 2:1, so simply dividing the name by 3 would (I think) work, i.e. if the name is exactly divisible by 3 or not could determine in which location to place it. But if I need to change that to e.g. 3:2 it's not so simple and the ratio needs to be flexible so that it can be easily set and/or changed for different setups. Ken, I think that the mod function would work, and would allow you to easily change the ratio in the future. In the present, you would use mod 3, and the answer would determine where to put the files, with 0 and 1 going onto one hard drive and 2 going onto the other. So, for filename 10, the remainder is 1, and it would go onto the first harddrive. Filename 11 (remainder 2) would go onto the second. Filename 12 (remainder 0) would go onto the first, etc. When you need to change the ratio, you simply change the details. In your example (ratio 3:2), you would use mod 5, and send remainders 0, 1 2 onto the one hard drive, and remainders 3 4 onto the other. That's very much what I had in mind, but I couldn't figure how to automatically set up which remainders go to which HD. I want just the ratio to be specified and everything else to be calculated. However, I think I can see how to do that now. Thanks for the replies that provided the inspirition. Ken G i l l e t t _/_/_/_/_/_/_/_/_/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting by number
On 18 Feb 2005 at 16:12, Ken Gillett wrote: That's very much what I had in mind, but I couldn't figure how to automatically set up which remainders go to which HD. I want just the ratio to be specified and everything else to be calculated. However, I think I can see how to do that now. Thanks for the replies that provided the inspirition. You're welcome. You could automate the process by using two variables to set the ratio, then calculating everything off that. If you need help in setting that up, give a holler. Doug -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting by number
Hi , what i understand from what you wrote ... that you want to check if the files already exits you won't to copy it again so you can check that useing if(-e location/Filename){do something } this will return true whenever a file is exits ... if you want to copy whenever a file isn't exits use it like this if(!-e location/Filename){ do something} this will do what you want like copying whenever a file is not exits ... i hope i got what you're looking for bye On Thu, 17 Feb 2005 11:27:46 +, Ken Gillett [EMAIL PROTECTED] wrote: I have a problem that is not exactly a perl problem, but I do have to implement it in perl, although the implementation is not the problem. Sorry if that's a bit obscure. I have thousands ( 50,000 ) of numerically named files that I need to store in 2 different location so that the ratio of numbers of files in each location is maintained at a preset ratio. The files will be repeatedly created and I want to ensure that each time this is done, the same named file is placed in the same location it was last time, therefore a simple alternating process or one that looks at the existing numbers of files would not be suitable, since at each generation of the files they would be unlikely to end up in the same place. So I really want something that examines the file name and makes a decision based on that, so that every time it sees the same name, it will be placed in the same location - unless there's another way to enforce the same rule? The file names are numbers between 10 and 10,000,000, with no grouping/order to the naming that would skew the result - assume an even distribution. Initially the storage requirements are 2:1, so simply dividing the name by 3 would (I think) work, i.e. if the name is exactly divisible by 3 or not could determine in which location to place it. But if I need to change that to e.g. 3:2 it's not so simple and the ratio needs to be flexible so that it can be easily set and/or changed for different setups. BTW, the background to this is that the list of files will be symlinks to (currently mostly mp3) music files and a larger number of far smaller supporting regular files (mostly text, some binary). The entire structure is to be created on the main server then rsync'd up to a player device which has 2 HDs for the storage, hence the 2 different locations and the need to ensure the files are in the same location every time, otherwise rsync will waste a lot of time deleting and re-copying files simply because they're in a different directory from last time and when playing with almost 100Gb of data this is to be avoided at all costs. Hope some of you can bring some brain power to work on this as my head hurts... Ken G i l l e t t _/_/_/_/_/_/_/_/_/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: splitting by number
Ken Gillett [EMAIL PROTECTED] wrote: : I have thousands ( 50,000 ) of numerically named files that I : need to store in 2 different location so that the ratio of : numbers of files in each location is maintained at a preset : ratio. The files will be repeatedly created and I want to ensure : that each time this is done, the same named file is placed in : the same location it was last time, therefore a simple : alternating process or one that looks at the existing numbers : of files would not be suitable, since at each generation of the : files they would be unlikely to end up in the same place. : : So I really want something that examines the file name and : makes a decision based on that, so that every time it sees the : same name, it will be placed in the same location - unless : there's another way to enforce the same rule? : : The file names are numbers between 10 and 10,000,000, with no : grouping/order to the naming that would skew the result - : assume an even distribution. : : Initially the storage requirements are 2:1, so simply dividing : the name by 3 would (I think) work, i.e. if the name is exactly : divisible by 3 or not could determine in which location to place : it. But if I need to change that to e.g. 3:2 it's not so simple : and the ratio needs to be flexible so that it can be easily set : and/or changed for different setups. A ratio is a fraction and can be turned into a floating point fairly easily. We can match this ratio to the file name we process. We would require the ratio and the maximum number of files. my $location = $filename location_threshold( '3/2', 10_000_000 ) ? 'larger location' : 'smaller location'; sub location_threshold { my( $ratio, $max_files ) = @_; my( $ratio_top, $ratio_bottom ) = split m|/|, $ratio; return int( $max_files * $ratio_top / ( $ratio_top + $ratio_bottom ) ) + 0.5; } We would end up with a value that directs the location by comparing the file name to the threshold. Testing lower than the threshold will yield the larger location as long as the ratio was expressed as larger value over smaller value. There is a chance that one or two files will be placed in the wrong location because this algorithm is not perfect, but no file name would end up in both locations. HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting by number
On 17 Feb 2005 at 11:27, Ken Gillett wrote: Initially the storage requirements are 2:1, so simply dividing the name by 3 would (I think) work, i.e. if the name is exactly divisible by 3 or not could determine in which location to place it. But if I need to change that to e.g. 3:2 it's not so simple and the ratio needs to be flexible so that it can be easily set and/or changed for different setups. Ken, I think that the mod function would work, and would allow you to easily change the ratio in the future. In the present, you would use mod 3, and the answer would determine where to put the files, with 0 and 1 going onto one hard drive and 2 going onto the other. So, for filename 10, the remainder is 1, and it would go onto the first harddrive. Filename 11 (remainder 2) would go onto the second. Filename 12 (remainder 0) would go onto the first, etc. When you need to change the ratio, you simply change the details. In your example (ratio 3:2), you would use mod 5, and send remainders 0, 1 2 onto the one hard drive, and remainders 3 4 onto the other. HTH Doug -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
re: Splitting large file into little chunks based on tagging (example script inc.)
Hi, I have a large file that I want to split into smaller chunks based on a start and end text (start...end) My script works only partially. It only saves the first occurence of my match and then closes the script. How can I get it to keep ripping throught the file saving into individual files. My file I'm reading in is called test.txt Below is just an example. blah...blah... blah...blah... blah...blah... start ... end blah...blah... blah...blah... blah...blah... start ... end and so on... Here is my script: #!/usr/bin/perl -w use strict; my $written = 0; my $index = 1; my $filename; if ( open( FH, 'D:\test.txt' ) ) { $filename = sprintf( 'D:\out%d.txt', $index ); open( OUT, $filename ); while ( FH ) { if ( m|^start$| ... m|^end$| ) { print OUT $_; $written = 1; } else { if ( $written ) { close( OUT ); $index++; $filename = sprintf( 'C:\out%d.txt', $index ); open( OUT, $filename ); $written = 0; } } print - $_; } close( FH ); } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting large file into little chunks based on tagging (example script inc.)
FlashMX wrote: Hi, Hello, I have a large file that I want to split into smaller chunks based on a start and end text (start...end) My script works only partially. It only saves the first occurence of my match and then closes the script. How can I get it to keep ripping throught the file saving into individual files. My file I'm reading in is called test.txt Below is just an example. blah...blah... blah...blah... blah...blah... start ... end blah...blah... blah...blah... blah...blah... start ... end and so on... Here is my script: #!/usr/bin/perl -w use strict; my $written = 0; my $index = 1; my $filename; if ( open( FH, 'D:\test.txt' ) ) { $filename = sprintf( 'D:\out%d.txt', $index ); open( OUT, $filename ); while ( FH ) { if ( m|^start$| ... m|^end$| ) { print OUT $_; $written = 1; } else { if ( $written ) { close( OUT ); $index++; $filename = sprintf( 'C:\out%d.txt', $index ); open( OUT, $filename ); $written = 0; } } print - $_; } close( FH ); } If I understand correctly then this should do what you want: (UNTESTED) if ( open FH, '', 'D:/test.txt' ) { while ( FH ) { my $range = /^start$/ .. /^end$/; if ( $range == 1 ) { my $filename = sprintf 'C:/out%d.txt', $index++; open OUT, '', $filename or die Cannot open $filename: $!; } if ( $range ) { print OUT; } else { print - $_; } } close FH; } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting colon delimited file in sub
William Paoli [EMAIL PROTECTED] wrote: : What is this doing? Very little. See below. : sub data_file { : open FILE, colon-delimted.txt; Always verify I/O operations. my $file = 'colon-delimted.txt'; open FH, $file or die qq(Cannot open $file: $!); : while ( my $line = FILE ) { Here we set $line = to each successive line in the file. :$line Here we assign something else to $line. Clobbering what was already there. : = ($driver,$sponsor,$owner,$chief,$car,$bio,$team) This part indicates that you either are not using 'strict' and 'warnings' or that you are deliberately affecting variables outside this sub's scope. : = split /:/, @_; Here you are splitting the number of elements passed to the subroutine. Since the integer does not have any colons in it, it only populates the first field ($driver). : } : } : am I on the right track? Probably not. What is the right track? The sub is equivalent to this. sub data_file { $driver = @_; $sponsor = undef; $owner = undef; $chief = undef; $car = undef; $bio = undef; $team= undef; return 1; } A well-written sub should generally not affect the external environment. This sub may be affecting the external variables $driver, $sponsor, $owner, $chief, $car, $bio, and $team. : I want to populate this into a hash after splitting it: What is this? Which field is the hash key? What is the name of the hash you are trying to create? How is data_file() being called? You cannot describe your problem in two questions and a short sentence. We need more information. When debugging a subroutine, we need to know how you are calling it. What data is going in, what data is coming out, and how you would like to have any incoming data processed. We are not clairvoyant. You have to tell us (in words) each detail of your problem for us to help you find a solution. HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting large xml file
Rob, Thanks for replying. I ended up answering my own question. I used XML::Twig to find chunks I was interested in, could grab indexing information from the twig, then save the indices in a database for later lookup of the entire XML record and...presto, random-access of 200 Mb of XML! Sean On Jul 22, 2004, at 7:06 PM, Hanson, Rob wrote: Ideally, I would use SAX to parse things Optionally you could look at XML::RAX. Article on the RAX concept: http://www.xml.com/pub/a/2000/04/26/rax/index.html RAX allows you to specify a record seperator (a tag in the XML file), and splits into into chunks of that tag. It is stream based so it only reads in as much of the file it needs to construct the next record. It only applies to XML files that fit that type of format though (like RSS). At the very least you might find the code helpful. but I can't figure out how to echo the data back out exactly as I got it. I'm not sure I completely understand. Anyway I am out of here today, hope you find an answer. Rob -Original Message- From: Sean Davis [mailto:[EMAIL PROTECTED] Sent: Thursday, July 22, 2004 5:42 PM To: [EMAIL PROTECTED] Subject: splitting large xml file I have a very large (200Mb) XML file that consists of multiple records. I would like to split these records up and store the XML for each in a database for quick retrieval. I simply need to echo all of the XML between the enclosing record tags into the database. Ideally, I would use SAX to parse things, but I can't figure out how to echo the data back out exactly as I got it. Any clues? Thanks, Sean -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: splitting large xml file
Ideally, I would use SAX to parse things Optionally you could look at XML::RAX. Article on the RAX concept: http://www.xml.com/pub/a/2000/04/26/rax/index.html RAX allows you to specify a record seperator (a tag in the XML file), and splits into into chunks of that tag. It is stream based so it only reads in as much of the file it needs to construct the next record. It only applies to XML files that fit that type of format though (like RSS). At the very least you might find the code helpful. but I can't figure out how to echo the data back out exactly as I got it. I'm not sure I completely understand. Anyway I am out of here today, hope you find an answer. Rob -Original Message- From: Sean Davis [mailto:[EMAIL PROTECTED] Sent: Thursday, July 22, 2004 5:42 PM To: [EMAIL PROTECTED] Subject: splitting large xml file I have a very large (200Mb) XML file that consists of multiple records. I would like to split these records up and store the XML for each in a database for quick retrieval. I simply need to echo all of the XML between the enclosing record tags into the database. Ideally, I would use SAX to parse things, but I can't figure out how to echo the data back out exactly as I got it. Any clues? Thanks, Sean -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting
aditi gupta wrote: hi to all, i have a file $seq, in following format: gi|37182815|gb|AY358849.1| gi|2353725|gb|AF015490.1|AF015490 100.00 16 0 0 544 559 320 335 4.2 32.21 gi|37182815|gb|AY358849.1| gi|1335960|gb|U55203.1|BTU55203 100.00 16 0 0 544 559 380 395 4.2 32.21 gi|37182815|gb|AY358849.1| gi|1335958|gb|U55202.1|BTU55202 100.00 16 0 0 544 559 443 458 4.2 32.21 i split it into arrays using: @seqs=split(/gi|37182815|gb|AY358849.1|/,$seq); split 'consumes' the pattern matched by its first argument. I think you probably want something like: (untested) @seq = split( /\|/, $seq ); and then split the last field on spaces: push @seq, split( /\s+/, pop(@seq) ); Regards, Randy. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting
Hi Aditi. During the split, what you have specifid is to match: gi OR 37182815 OR gb OR AY358849.1 ... That's what is giving the result you have. I.e. the | means OR in a regexp like the one used in split... Easiest solution is to escape the pipe character, i.e.: @seqs=split(/gi\|37182815\|gb\|AY358849.1\|/,$seq); Should lead you closer to a solution to your headaches.. //Anders// On Fri, 2004-06-11 at 08:51, aditi gupta wrote: hi to all, i have a file $seq, in following format: gi|37182815|gb|AY358849.1| gi|2353725|gb|AF015490.1|AF015490 100.00 16 0 0 544 559 320 335 4.2 32.21 gi|37182815|gb|AY358849.1| gi|1335960|gb|U55203.1|BTU55203 100.00 16 0 0 544 559 380 395 4.2 32.21 gi|37182815|gb|AY358849.1| gi|1335958|gb|U55202.1|BTU55202 100.00 16 0 0 544 559 443 458 4.2 32.21 i split it into arrays using: @seqs=split(/gi|37182815|gb|AY358849.1|/,$seq); but when i printed the array,it was: |2353725||AF015490.1|AF015490 100.00 16 0 0 544 559 320 335 4.2 32.21 |1335960||U55203.1|BTU55203 100.00 16 0 0 544 559 380 395 4.2 32.21 |1335958||U55202.1|BTU55202 100.00 16 0 0 544 559 443 458 4.2 32.21 i.e. the second occurance of gi is also being deleted. Also ' | ' separators present in /gi|37182815|gb|AY358849.1|/ is still present.can these be removed?and can the second gi,since it is an accession no. and will be required later, be from deletion? please help!! -aditi Yahoo! India Matrimony: Find your partner online. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting
If you split the line like this: @seqs=split(/gi|37182815|gb|AY358849.1|/,$seq); It means the fields are separated by 'gi' or '37182815' or 'gb' or 'AY358849.1'. I don't think this is what you are looking for... From the previous post, it seems the file is separated by tabs, so 'gi|37182815|gb|AY358849.1|' is the first field. Then, to get the fields all you need to do is: @seqs=split(/\t/,$seq); aditi gupta wrote: hi to all, i have a file $seq, in following format: gi|37182815|gb|AY358849.1| gi|2353725|gb|AF015490.1|AF015490 100.00 16 0 0 544 559 320 335 4.2 32.21 gi|37182815|gb|AY358849.1| gi|1335960|gb|U55203.1|BTU55203 100.00 16 0 0 544 559 380 395 4.2 32.21 gi|37182815|gb|AY358849.1| gi|1335958|gb|U55202.1|BTU55202 100.00 16 0 0 544 559 443 458 4.2 32.21 i split it into arrays using: @seqs=split(/gi|37182815|gb|AY358849.1|/,$seq); but when i printed the array,it was: |2353725||AF015490.1|AF015490 100.00 16 0 0 544 559 320 335 4.2 32.21 |1335960||U55203.1|BTU55203 100.00 16 0 0 544 559 380 395 4.2 32.21 |1335958||U55202.1|BTU55202 100.00 16 0 0 544 559 443 458 4.2 32.21 i.e. the second occurance of gi is also being deleted. Also ' | ' separators present in /gi|37182815|gb|AY358849.1|/ is still present.can these be removed?and can the second gi,since it is an accession no. and will be required later, be from deletion? please help!! -aditi Yahoo! India Matrimony: Find your partner online. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting with special characters
On Jun 3, 2004, at 8:11 AM, Singh, Ajit p wrote: Hello All, I am trying to split a string with the / ( forward slash) as the marker. $mystring = abcde/fghi split (///,$mystring) -- gives me compile error split (/\//,$mystring) -- gives me abcdefghi I hope not. The second one is fine: perl -e 'print map { [ $_ ]\n } split /\//, abcde/fghi' [ abcde ] [ fghi ] I suspect something else is going on you're not telling us about. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: splitting with special characters
Hi Singh, Try this: $mystring = abcde/fghi; @a = split (/\//, $mystring); print $a[0]\n; print $a[1]; Regards, Cristi Ocolisan -Original Message- From: Singh, Ajit p [mailto:[EMAIL PROTECTED] Sent: 3 iunie 2004 16:11 To: Singh, Ajit p; [EMAIL PROTECTED] Subject: splitting with special characters Hello All, I am trying to split a string with the / ( forward slash) as the marker. $mystring = abcde/fghi split (///,$mystring) -- gives me compile error split (/\//,$mystring) -- gives me abcdefghi how do i specify / as the delimitter. regards, Ajitpal Singh, -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting with special characters
Works for me #!/usr/bin/perl # # use strict; use Data::Dumper; my $mystring = abcde/fghi; my @a = split (/\//,$mystring); print Dumper([EMAIL PROTECTED]); __END__ OUTPUT $VAR1 = [ 'abcde', 'fghi' ]; Ram On Thu, 2004-06-03 at 18:41, Singh, Ajit p wrote: Hello All, I am trying to split a string with the / ( forward slash) as the marker. $mystring = abcde/fghi split (///,$mystring) -- gives me compile error split (/\//,$mystring) -- gives me abcdefghi how do i specify / as the delimitter. regards, Ajitpal Singh, -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting with special characters
I think whats happening is your printing the entire array @result_from_split...do @result_from_split[0] and so on.. Mandar Original message Date: Thu, 3 Jun 2004 08:19:45 -0500 From: James Edward Gray II [EMAIL PROTECTED] Subject: Re: splitting with special characters To: Singh, Ajit p [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] On Jun 3, 2004, at 8:11 AM, Singh, Ajit p wrote: Hello All, I am trying to split a string with the / ( forward slash) as the marker. $mystring = abcde/fghi split (///,$mystring) -- gives me compile error split (/\//,$mystring) -- gives me abcdefghi I hope not. The second one is fine: perl -e 'print map { [ $_ ]\n } split /\//, abcde/fghi' [ abcde ] [ fghi ] I suspect something else is going on you're not telling us about. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response --- Mandar Rahurkar ECE, UIUC --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: splitting with special characters
-Original Message- From: Singh, Ajit p [mailto:[EMAIL PROTECTED] Sent: Thursday, June 03, 2004 3:11 PM To: Singh, Ajit p; [EMAIL PROTECTED] Subject: splitting with special characters Hello All, I am trying to split a string with the / ( forward slash) as the marker. $mystring = abcde/fghi split (///,$mystring) -- gives me compile error split (/\//,$mystring) -- gives me abcdefghi Your syntax is ok. split (/\//,$mystring) actually retuns an array. In your case this array will be ('abcde', 'fghi'). use Data::Dumper; my @array=split /\//,$mystring; print Dumper [EMAIL PROTECTED]; #you did probably #print @array; #that's why you have seen abcdefghi ;) José. DISCLAIMER This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer. Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting html into body and header...
juman wrote: Does anyoen have a good tip if there is a module or good way to split a html file inte header and body? What I want to do is to combine several htmlfiles into one. And the idea is to remove all headers from all the files and combine the bodys into a big new one with a new header... /juman I've used Mail::Internet in the past in combination with procmail. Its really simple to use, just have procmail direct the mail to your script and it will parse it as STDIN. Simple my $mail = Mail::Internet-new(\*STDIN); my $headers = $mail-head-header_hashref; my @body = @{$mail-tidy_body()}; my $SUBJECT = $headers-{Subject}-[0] ; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting html into body and header...
It was Wednesday, February 25, 2004 when David O'Dell took the soap box, saying: : juman wrote: : Does anyoen have a good tip if there is a module or good way to split a : html file inte header and body? What I want to do is to combine several : htmlfiles into one. And the idea is to remove all headers from all the : files and combine the bodys into a big new one with a new header... : : I've used Mail::Internet in the past in combination with procmail. : Its really simple to use, just have procmail direct the mail to your : script and it will parse it as STDIN. We're not talking about email here, but about HTML. :-) HTML::TreeBuilder is a good module to look at. Casey West -- Shooting yourself in the foot with English You put your foot in your mouth, then bite it off. (For those who don't know, English is a McDonnell Douglas/PICK query language which allegedly requires 110% of system resources to run happily.) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting html into body and header...
Okay.. cpan.org here I come :) /juman On Wed, Feb 25, 2004 at 05:47:18PM -0500, Casey West wrote: HTML::TreeBuilder is a good module to look at. Casey West -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting / unpacking line into array
John McKown wrote: On Mon, 2 Feb 2004, Wiggins d Anconia wrote: Sounds pretty good to me. One concern, do the sub record types always have the same number of fields? Using your array to unpack into may turn into a maintenance nightmare with respect to indexing into it to get values if the record formats are signficantly different, etc. Actually, that was only an example. I really hope to have the result returned more like: if ($subrec = '0100') { ($name, $address, $city ) = unpack $template{$subrec}, $_ ; } elsif ($subrec = '0101') { ($some1, $some2) = unpack $template{$subrec}, $_; } and so on for each defined $subrec. That works, though you have to repeat your unpack over and over (not a big deal) but using the slices you only need it once and don't have to check the subrec type, though you will again when you use them...unless again you push to an array in a hash where the key is the subtype and then just loop over each of the different types, which might make the code more modular, granted the data structure would be more complicated (and unordered at that point). Second concern, are you processing the records completely within the loop or needing to parse them all before doing anything with them? In the latter case you may need to store them to an array based on type rather than directly to a 'values' temporary array, etc. I will be processing the records one at a time and putting them in a persistant storage for retrieval later in a reporting program. I have not yet determined what sort of persistant storage that I want. Perhaps DBM, perhaps PostgreSQL, perhaps mySQL, whatever. I may end up not even doing this since PostgreSQL, at least, has a way to load records from a flat file. I just like to leave my options open. And I'm looking a Perl solutions right now mainly because I'm trying to learn Perl. MySQL can load flat files as well, though I don't know about formatted files like you describe. off-topic Also, if I find a nice Perl solution, I may implement it in production on our mainframe (IBM zSeries) at work. The actual data being parsed is a RACF (security system) database unload. If I can ftp that data from z/OS to our Linux/390 system and do all my reporting there, I can save z/OS CPU utilization. That's because Linux/390 on our zSeries runs on a separate processor from the z/OS work. The z/OS work cannot use this processor due to licensing restrictions. So, any work that I can offload from z/OS is a net gain because the IFL (Linux processor) is basically idle right now. I would then use Perl to create reports which would then be ftp'ed back to the z/OS system. This gets me brownie points by offloading z/OS processing. We are critically short of z/OS processor power and the next upgrade would cost 1.5 million dollars in software upgrade fees. Yikes, I understood just enough of that to know that I am running for the hills :-)... Though I will say that it should be doable, and I assume you have checked out Net::FTP... If this works for the database unload, I can use a similar system for RACF reports run against the reformatted audit logs. Again, getting brownie points for offloading work. This is why I'm considering a Perl-only solution. I have Perl on our SuSE Linux/390 system. I do not have any SQL database and am not really good enough to try to port something like PostgreSQL or mySQL. If you decide against using a real database you might consider using some of the CSV text file modules, there is even a DBD::CSV that will allow you to implement using real SQL and the DBI if in the future you might get to port to a database and don't want to change the code later, though it is not speedy by any means. There is also XML, but that is all I will say for now :-) /off-topic For the first concern you may consider using a hash slice with the keys being associated with the subtype stored in the original hash where you retrieve the record format from. Good idea. I'll keep it in mind. thanks much! Good luck, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting / unpacking line into array
I have an input file which has many subrecords. The subrecord type is denoted by the first 4 characters of the file. The rest of the line is formatted like similar to the way that pack would format one. That is, each data point in a subtype is always at the same offset for the same length. E.g. 10 characters starting at offset 30, or some such. What I'm considering is using unpack and having a hash contain the unpack template based on the subrecord type. Something like: while (FH) { my $subrec = substr($_,0,4); my @values = unpack $template{$subrec}, $_; ... } Earlier in the code, I would have created the %template hash which would have the template associated with the $subrec from the input file. Is this a decent way to do this? Is there a better way? Sounds pretty good to me. One concern, do the sub record types always have the same number of fields? Using your array to unpack into may turn into a maintenance nightmare with respect to indexing into it to get values if the record formats are signficantly different, etc. Second concern, are you processing the records completely within the loop or needing to parse them all before doing anything with them? In the latter case you may need to store them to an array based on type rather than directly to a 'values' temporary array, etc. For the first concern you may consider using a hash slice with the keys being associated with the subtype stored in the original hash where you retrieve the record format from. Obviously there is also the potential to use objects here but that may be overkill depending on what you are doing with the data after you have unpacked it http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting / unpacking line into array
On Mon, 2 Feb 2004, Wiggins d Anconia wrote: Sounds pretty good to me. One concern, do the sub record types always have the same number of fields? Using your array to unpack into may turn into a maintenance nightmare with respect to indexing into it to get values if the record formats are signficantly different, etc. Actually, that was only an example. I really hope to have the result returned more like: if ($subrec = '0100') { ($name, $address, $city ) = unpack $template{$subrec}, $_ ; } elsif ($subrec = '0101') { ($some1, $some2) = unpack $template{$subrec}, $_; } and so on for each defined $subrec. Second concern, are you processing the records completely within the loop or needing to parse them all before doing anything with them? In the latter case you may need to store them to an array based on type rather than directly to a 'values' temporary array, etc. I will be processing the records one at a time and putting them in a persistant storage for retrieval later in a reporting program. I have not yet determined what sort of persistant storage that I want. Perhaps DBM, perhaps PostgreSQL, perhaps mySQL, whatever. I may end up not even doing this since PostgreSQL, at least, has a way to load records from a flat file. I just like to leave my options open. And I'm looking a Perl solutions right now mainly because I'm trying to learn Perl. off-topic Also, if I find a nice Perl solution, I may implement it in production on our mainframe (IBM zSeries) at work. The actual data being parsed is a RACF (security system) database unload. If I can ftp that data from z/OS to our Linux/390 system and do all my reporting there, I can save z/OS CPU utilization. That's because Linux/390 on our zSeries runs on a separate processor from the z/OS work. The z/OS work cannot use this processor due to licensing restrictions. So, any work that I can offload from z/OS is a net gain because the IFL (Linux processor) is basically idle right now. I would then use Perl to create reports which would then be ftp'ed back to the z/OS system. This gets me brownie points by offloading z/OS processing. We are critically short of z/OS processor power and the next upgrade would cost 1.5 million dollars in software upgrade fees. If this works for the database unload, I can use a similar system for RACF reports run against the reformatted audit logs. Again, getting brownie points for offloading work. This is why I'm considering a Perl-only solution. I have Perl on our SuSE Linux/390 system. I do not have any SQL database and am not really good enough to try to port something like PostgreSQL or mySQL. /off-topic For the first concern you may consider using a hash slice with the keys being associated with the subtype stored in the original hash where you retrieve the record format from. Good idea. I'll keep it in mind. thanks much! -- Maranatha! John McKown -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting Comma delimited list
On Dec 29, 2003, at 3:12 PM, Colin Johnstone wrote: Hello everyone, I use the following to split a comma delimited list and get varying results and I don't understand why. Running your code on your data, I get the expected results (43). my count of the elements in the splitdata array returns 43 but the select list when displayed in our Content management system only has approximately 23 items. Some items are compltely ignored. The comma delimited txt file is below. Could this be a browser issue? Is the XHTML still correct? About You,BCS Employee,Sales Recognition Events Framework,BPFJ+,Building Wealth,Community Program,Compensation,Employment Confirmations,Employee Development,Employee Services,Executive Development,Expense Reimbursement,Financial Benefits,Flexibility,Global Pulse Survey,Health Benefits,Hiring,HR Assist,IDP Tool,Insurance,Manager Services,Management Development,Mobility,New Hire Information,Other,PaylinkPlus,Payroll Services,PBC Tool - Urgent,PBC Tool - Normal,Peace of Mind Benefits,Personnel Records,Promotions,Reporting,Reporting - Data Quality Integrity,Site Utilities,Single Cycle Salary Review,Transition Employees,Transactional,Well Being,When Life Changes,Workforce Diversity,Workplace Practices,Your Career #!/usr/bin/perl # I strongly recommend... use strict; use warnings; my $fileToOpen = shift; # $string = qq[option value=$fileToOpen label=$fileToOpen/]; open( FILEHANDLE, $fileToOpen ) or die (Could not open file $fileToOpen); # You'll stay sane longer if you think up file handle names better than FILEHANDLE while( my $line = FILEHANDLE ){ chomp $line; $inFile .= $line; } # you can replace the above loop with a single read undef $/; # enter slurp mode my $inFile = FILEHANDLE; close(FILEHANDLE); my @splitData = split( /\,/, $inFile ); $numElements = scalar(@splitData); $optionHTML = qq[option label=$numElements: /]; foreach $subject(@splitData){ $subject =~ s//amp;/g; $subject =~ s/\/quot;/g; $optionHTML .= qq[option label=$subject /]; } # produce output print EOF; ?xml version=1.0 encoding='UTF-8'? substitution $optionHTML /substitution EOF # -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting Comma delimited list
Hi James, Thanks for the tips, I took your advice here is the HTML returned, There still isn't the full 43 elements though strange? select class='multiSelectStyle' name='anotherContainer/Choose Page Subject' size=1 onfocus='datacapture.handleElementSelect( 26);' onchange='parent.api.iw_dispatch_handleOnChange(26,this);' option value= option value=About You About You option value=Sales Recognition Events Framework Sales Recognition Events Framework option value=Building Wealth Building Wealth option value=Compensation Compensation option value=Employee Development Employee Development option value=Executive Development Executive Development option value=Financial Benefits Financial Benefits option value=Global Pulse Survey Global Pulse Survey option value=Hiring Hiring option value=IDP Tool IDP Tool option value=Manager Services Manager Services option value=Mobility Mobility option value=Other Other option value=Payroll Services Payroll Services option value=PBC Tool - Normal PBC Tool - Normal option value=Personnel Records Personnel Records option value=Reporting Reporting option value=Site Utilities Site Utilities option value=Transition Employees Transition Employees option value=Well Being Well Being option value=Workforce Diversity Workforce Diversity option value=Your Career Your Career /select Cheers Colin James Edward Gray II [EMAIL PROTECTED] 30/12/2003 08:26 AM To: Colin Johnstone/Australia/Contr/[EMAIL PROTECTED] cc: [EMAIL PROTECTED] Subject:Re: Splitting Comma delimited list On Dec 29, 2003, at 3:12 PM, Colin Johnstone wrote: Hello everyone, I use the following to split a comma delimited list and get varying results and I don't understand why. Running your code on your data, I get the expected results (43). my count of the elements in the splitdata array returns 43 but the select list when displayed in our Content management system only has approximately 23 items. Some items are compltely ignored. The comma delimited txt file is below. Could this be a browser issue? Is the XHTML still correct? About You,BCS Employee,Sales Recognition Events Framework,BPFJ+,Building Wealth,Community Program,Compensation,Employment Confirmations,Employee Development,Employee Services,Executive Development,Expense Reimbursement,Financial Benefits,Flexibility,Global Pulse Survey,Health Benefits,Hiring,HR Assist,IDP Tool,Insurance,Manager Services,Management Development,Mobility,New Hire Information,Other,PaylinkPlus,Payroll Services,PBC Tool - Urgent,PBC Tool - Normal,Peace of Mind Benefits,Personnel Records,Promotions,Reporting,Reporting - Data Quality Integrity,Site Utilities,Single Cycle Salary Review,Transition Employees,Transactional,Well Being,When Life Changes,Workforce Diversity,Workplace Practices,Your Career #!/usr/bin/perl # I strongly recommend... use strict; use warnings; my $fileToOpen = shift; # $string = qq[option value=$fileToOpen label=$fileToOpen/]; open( FILEHANDLE, $fileToOpen ) or die (Could not open file $fileToOpen); # You'll stay sane longer if you think up file handle names better than FILEHANDLE while( my $line = FILEHANDLE ){ chomp $line; $inFile .= $line; } # you can replace the above loop with a single read undef $/;# enter slurp mode my $inFile = FILEHANDLE; close(FILEHANDLE); my @splitData = split( /\,/, $inFile ); $numElements = scalar(@splitData); $optionHTML = qq[option label=$numElements: /]; foreach $subject(@splitData){ $subject =~ s//amp;/g; $subject =~ s/\/quot;/g; $optionHTML .= qq[option label=$subject /]; } # produce output print EOF; ?xml version=1.0 encoding='UTF-8'? substitution $optionHTML /substitution EOF # -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting Comma delimited list
On Dec 29, 2003, at 3:40 PM, Colin Johnstone wrote: Hi James, Thanks for the tips, I took your advice here is the HTML returned, There still isn't the full 43 elements though strange? So you obviously sent us: A. Different Data or B. Different Code. Right? ;) That might put us at a slight disadvantage. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting Comma delimited list
It looks like you are skipping exactly every other element. This is most likely a coding error in an area of the code you're not showing us or inadvertently fixed when you copied it to the email. I would check for a place where you might be shift()ing or otherwise pulling an element out and assigning it to a variable twice by accident so that one result gets thrown away, or something along those lines. -Original Message- From: Colin Johnstone [mailto:[EMAIL PROTECTED] Sent: Monday, December 29, 2003 1:40 PM To: James Edward Gray II Cc: [EMAIL PROTECTED] Subject: Re: Splitting Comma delimited list Hi James, Thanks for the tips, I took your advice here is the HTML returned, There still isn't the full 43 elements though strange? select class='multiSelectStyle' name='anotherContainer/Choose Page Subject' size=1 onfocus='datacapture.handleElementSelect( 26);' onchange='parent.api.iw_dispatch_handleOnChange(26,this);' option value= option value=About You About You option value=Sales Recognition Events Framework Sales Recognition Events Framework option value=Building Wealth Building Wealth option value=Compensation Compensation option value=Employee Development Employee Development option value=Executive Development Executive Development option value=Financial Benefits Financial Benefits option value=Global Pulse Survey Global Pulse Survey option value=Hiring Hiring option value=IDP Tool IDP Tool option value=Manager Services Manager Services option value=Mobility Mobility option value=Other Other option value=Payroll Services Payroll Services option value=PBC Tool - Normal PBC Tool - Normal option value=Personnel Records Personnel Records option value=Reporting Reporting option value=Site Utilities Site Utilities option value=Transition Employees Transition Employees option value=Well Being Well Being option value=Workforce Diversity Workforce Diversity option value=Your Career Your Career /select Cheers Colin snip About You,BCS Employee,Sales Recognition Events Framework,BPFJ+,Building Wealth,Community Program,Compensation,Employment Confirmations,Employee Development,Employee Services,Executive Development,Expense Reimbursement,Financial Benefits,Flexibility,Global Pulse Survey,Health Benefits,Hiring,HR Assist,IDP Tool,Insurance,Manager Services,Management Development,Mobility,New Hire Information,Other,PaylinkPlus,Payroll Services,PBC Tool - Urgent,PBC Tool - Normal,Peace of Mind Benefits,Personnel Records,Promotions,Reporting,Reporting - Data Quality Integrity,Site Utilities,Single Cycle Salary Review,Transition Employees,Transactional,Well Being,When Life Changes,Workforce Diversity,Workplace Practices,Your Career /snip -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting Comma delimited list
Hi All, I have nothing to gain by not showing you all my code, I have included all suggested changes and even am writing to the select list the number of elements. note the second item. here is my code again with the changes and the output below, note the items in the list this time are different from the first I think Tim may be onto something but can't see where. Cheers Colin #!c:/iw-home/iw-perl/bin/iwperl my $fileToOpen = shift; # $string = qq[option value=$fileToOpen label=$fileToOpen/]; open( FILEHANDLE, $fileToOpen ) or die (Could not open file $fileToOpen); undef $/; # enter slurp mode my $inFile = FILEHANDLE; close( FILEHANDLE ); my @splitData = split( /\,/, $inFile ); $numElements = scalar(@splitData); $optionHTML = qq[option label=$numElements /]; foreach $subject ( @splitData ) { $subject = xc($subject); $optionHTML .= qq[option label=$subject /]; } # produce output print EOF; ?xml version=1.0 encoding='UTF-8'? substitution $optionHTML /substitution EOF # sub xc { #returns text free of XML baddies - xc = xml clean my $data = $_[0]; $data =~ s//amp;/g; $data =~ s//lt;/g; $data =~ s//gt;/g; $data =~ s/'/apos;/g; $data =~ s//quot;/g; return $data; } and the output: select class='multiSelectStyle' name='anotherContainer/Choose Page Subject' size=1 onfocus='datacapture.handleElementSelect( 26);' onchange='parent.api.iw_dispatch_handleOnChange(26,this);' option value= option value=43 43 option value=BCS Employee BCS Employee option value=BPFJ+ BPFJ+ option value=Community Program Community Program option value=Employment Confirmations Employment Confirmations option value=Employee Services Employee Services option value=Expense Reimbursement Expense Reimbursement option value=Flexibility Flexibility option value=Health Benefits Health Benefits option value=HR Assist HR Assist option value=Insurance Insurance option value=Management Development Management Development option value=New Hire Information New Hire Information option value=PaylinkPlus PaylinkPlus option value=PBC Tool - Urgent PBC Tool - Urgent option value=Peace of Mind Benefits Peace of Mind Benefits option value=Promotions Promotions option value=Reporting - Data Quality amp; Integrity Reporting - Data Quality amp; Integrity option value=Single Cycle Salary Review Single Cycle Salary Review option value=Transactional Transactional option value=When Life Changes When Life Changes option value=Workplace Practices Workplace Practices /select Tim Johnson [EMAIL PROTECTED] 30/12/2003 08:49 AM To: Colin Johnstone/Australia/Contr/[EMAIL PROTECTED], James Edward Gray II [EMAIL PROTECTED] cc: [EMAIL PROTECTED] Subject:RE: Splitting Comma delimited list It looks like you are skipping exactly every other element. This is most likely a coding error in an area of the code you're not showing us or inadvertently fixed when you copied it to the email. I would check for a place where you might be shift()ing or otherwise pulling an element out and assigning it to a variable twice by accident so that one result gets thrown away, or something along those lines. -Original Message- From: Colin Johnstone [mailto:[EMAIL PROTECTED] Sent: Monday, December 29, 2003 1:40 PM To: James Edward Gray II Cc: [EMAIL PROTECTED] Subject: Re: Splitting Comma delimited list Hi James, Thanks for the tips, I took your advice here is the HTML returned, There still isn't the full 43 elements though strange? select class='multiSelectStyle' name='anotherContainer/Choose Page Subject' size=1 onfocus='datacapture.handleElementSelect( 26);' onchange='parent.api.iw_dispatch_handleOnChange(26,this);' option value= option value=About You About You option value=Sales Recognition Events Framework Sales Recognition Events Framework option value=Building Wealth Building Wealth option value=Compensation Compensation option value=Employee Development Employee Development option value=Executive Development Executive Development option value=Financial Benefits Financial Benefits option value=Global Pulse Survey Global Pulse Survey option value=Hiring Hiring option value=IDP Tool IDP Tool option value=Manager Services Manager Services option value=Mobility Mobility option value=Other Other option value=Payroll Services Payroll Services option value=PBC Tool - Normal PBC Tool - Normal option value=Personnel Records Personnel Records option value=Reporting Reporting option value=Site Utilities Site Utilities option value=Transition Employees Transition Employees option value=Well Being Well Being option value=Workforce Diversity Workforce Diversity option value=Your Career Your Career /select Cheers Colin snip About You,BCS Employee,Sales Recognition Events Framework,BPFJ+,Building Wealth,Community Program,Compensation,Employment Confirmations,Employee Development,Employee Services,Executive Development,Expense
RE: Splitting Comma delimited list
Colin Johnstone wrote: Hi All, I have nothing to gain by not showing you all my code, I have included all suggested changes and even am writing to the select list the number of elements. note the second item. here is my code again with the changes and the output below, note the items in the list this time are different from the first I think Tim may be onto something but can't see where. Cheers Colin #!c:/iw-home/iw-perl/bin/iwperl my $fileToOpen = shift; # $string = qq[option value=$fileToOpen label=$fileToOpen/]; open( FILEHANDLE, $fileToOpen ) or die (Could not open file $fileToOpen); undef $/; # enter slurp mode my $inFile = FILEHANDLE; close( FILEHANDLE ); my @splitData = split( /\,/, $inFile ); $numElements = scalar(@splitData); $optionHTML = qq[option label=$numElements /]; foreach $subject ( @splitData ) { $subject = xc($subject); $optionHTML .= qq[option label=$subject /]; } # produce output print EOF; ?xml version=1.0 encoding='UTF-8'? substitution $optionHTML /substitution EOF # sub xc { #returns text free of XML baddies - xc = xml clean my $data = $_[0]; $data =~ s//amp;/g; $data =~ s//lt;/g; $data =~ s//gt;/g; $data =~ s/'/apos;/g; $data =~ s//quot;/g; return $data; } and the output: select class='multiSelectStyle' name='anotherContainer/Choose Page Subject' size=1 onfocus='datacapture.handleElementSelect( 26);' onchange='parent.api.iw_dispatch_handleOnChange(26,this);' option value= option value=43 43 option value=BCS Employee BCS Employee option value=BPFJ+ BPFJ+ option value=Community Program Community Program option value=Employment Confirmations Employment Confirmations option value=Employee Services Employee Services option value=Expense Reimbursement Expense Reimbursement option value=Flexibility Flexibility option value=Health Benefits Health Benefits option value=HR Assist HR Assist option value=Insurance Insurance option value=Management Development Management Development option value=New Hire Information New Hire Information option value=PaylinkPlus PaylinkPlus option value=PBC Tool - Urgent PBC Tool - Urgent option value=Peace of Mind Benefits Peace of Mind Benefits option value=Promotions Promotions option value=Reporting - Data Quality amp; Integrity Reporting - Data Quality amp; Integrity option value=Single Cycle Salary Review Single Cycle Salary Review option value=Transactional Transactional option value=When Life Changes When Life Changes option value=Workplace Practices Workplace Practices /select Something's fishy. That code could not possibly have produced that output. You add an option with: $optionHTML .= qq[option label=$subject /]; But your output lines look like: option value=BCS Employee BCS Employee When they should look like (according to the code above): option label=BCS Employee / Perhaps you're not running the code you *think* you're running... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Splitting Comma delimited list
Bob, The cms makes those changes. It also inserts comments as shown below which aren't in my code and wraps it all in the select tags. DIV id=ctl26!-- Begin anotherContainer/Choose Page Subject-- select class='multiSelectStyle' name='anotherContainer/Choose Page Subject' size=1 onfocus='datacapture.handleElementSelect( 26);' onchange='parent.api.iw_dispatch_handleOnChange(26,this);' option value= option value=43 43 option value=BCS Employee BCS Employee option value=BPFJ+ BPFJ+ option value=Community Program Community Program option value=Employment Confirmations Employment Confirmations option value=Employee Services Employee Services option value=Expense Reimbursement Expense Reimbursement option value=Flexibility Flexibility option value=Health Benefits Health Benefits option value=HR Assist HR Assist option value=Insurance Insurance option value=Management Development Management Development option value=New Hire Information New Hire Information option value=PaylinkPlus PaylinkPlus option value=PBC Tool - Urgent PBC Tool - Urgent option value=Peace of Mind Benefits Peace of Mind Benefits option value=Promotions Promotions option value=Reporting - Data Quality amp; Integrity Reporting - Data Quality amp; Integrity option value=Single Cycle Salary Review Single Cycle Salary Review option value=Transactional Transactional option value=When Life Changes When Life Changes option value=Workplace Practices Workplace Practices /select !-- End anotherContainer/Choose Page Subject-- !-- End DIV for ctl26-- /DIV Cheers Colin Bob Showalter [EMAIL PROTECTED] 30/12/2003 09:23 AM To: Colin Johnstone/Australia/Contr/[EMAIL PROTECTED] cc: [EMAIL PROTECTED] Subject:RE: Splitting Comma delimited list Colin Johnstone wrote: Hi All, I have nothing to gain by not showing you all my code, I have included all suggested changes and even am writing to the select list the number of elements. note the second item. here is my code again with the changes and the output below, note the items in the list this time are different from the first I think Tim may be onto something but can't see where. Cheers Colin #!c:/iw-home/iw-perl/bin/iwperl my $fileToOpen = shift; # $string = qq[option value=$fileToOpen label=$fileToOpen/]; open( FILEHANDLE, $fileToOpen ) or die (Could not open file $fileToOpen); undef $/; # enter slurp mode my $inFile = FILEHANDLE; close( FILEHANDLE ); my @splitData = split( /\,/, $inFile ); $numElements = scalar(@splitData); $optionHTML = qq[option label=$numElements /]; foreach $subject ( @splitData ) { $subject = xc($subject); $optionHTML .= qq[option label=$subject /]; } # produce output print EOF; ?xml version=1.0 encoding='UTF-8'? substitution $optionHTML /substitution EOF # sub xc { #returns text free of XML baddies - xc = xml clean my $data = $_[0]; $data =~ s//amp;/g; $data =~ s//lt;/g; $data =~ s//gt;/g; $data =~ s/'/apos;/g; $data =~ s//quot;/g; return $data; } and the output: select class='multiSelectStyle' name='anotherContainer/Choose Page Subject' size=1 onfocus='datacapture.handleElementSelect( 26);' onchange='parent.api.iw_dispatch_handleOnChange(26,this);' option value= option value=43 43 option value=BCS Employee BCS Employee option value=BPFJ+ BPFJ+ option value=Community Program Community Program option value=Employment Confirmations Employment Confirmations option value=Employee Services Employee Services option value=Expense Reimbursement Expense Reimbursement option value=Flexibility Flexibility option value=Health Benefits Health Benefits option value=HR Assist HR Assist option value=Insurance Insurance option value=Management Development Management Development option value=New Hire Information New Hire Information option value=PaylinkPlus PaylinkPlus option value=PBC Tool - Urgent PBC Tool - Urgent option value=Peace of Mind Benefits Peace of Mind Benefits option value=Promotions Promotions option value=Reporting - Data Quality amp; Integrity Reporting - Data Quality amp; Integrity option value=Single Cycle Salary Review Single Cycle Salary Review option value=Transactional Transactional option value=When Life Changes When Life Changes option value=Workplace Practices Workplace Practices /select Something's fishy. That code could not possibly have produced that output. You add an option with: $optionHTML .= qq[option label=$subject /]; But your output lines look like: option value=BCS Employee BCS Employee When they should look like (according to the code above): option label=BCS Employee / Perhaps you're not running the code you *think* you're running... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting Comma delimited list
Colin Johnstone wrote: Bob, The cms makes those changes. It also inserts comments as shown below which aren't in my code and wraps it all in the select tags. A suggestion: Post a message with the input data, the script, and the actual output produced *by that script*. Put the input data under a __DATA__ tag. Leave the cms out of it for now. That way we can run your self-contained script and see what's going on. Heck, you may even find the problem while going through this exercise :~) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting Comma delimited list
HI Bob, rather than persist I changed the code so that it would write each option tag to an array then I print out the array in the heredoc and it works. Be buggered if I know what the previous problem was though. Thanks all for your help #!c:/iw-home/iw-perl/bin/iwperl my $fileToOpen = shift; open( FILEHANDLE, $fileToOpen ) or die (Could not open file $fileToOpen); undef $/; # enter slurp mode my $inFile = FILEHANDLE; close( FILEHANDLE ); my @splitData = split( /\,/, $inFile ); foreach $subject ( @splitData ) { $subject = xmlEncode($subject); $optionHTML = qq[option label=$subject /]; push( @arrayOptions, $optionHTML ); } # produce output print EOF; ?xml version=1.0 encoding='UTF-8'? substitution @arrayOptions /substitution EOF # sub xmlEncode { #returns text free of XML baddies - xc = xml clean my $data = $_[0]; $data =~ s//amp;/g; $data =~ s//lt;/g; $data =~ s//gt;/g; $data =~ s/\'/apos;/g; $data =~ s/\/quot;/g; return $data; } __data__ About You,BCS Employee,Sales Recognition Events Framework,BPFJ+,Building Wealth,Community Program,Compensation,Employment Confirmations,Employee Development,Employee Services,Executive Development,Expense Reimbursement,Financial Benefits,Flexibility,Global Pulse Survey,Health Benefits,Hiring,HR Assist,IDP Tool,Insurance,Manager Services,Management Development,Mobility,New Hire Information,Other,PaylinkPlus,Payroll Services,PBC Tool - Urgent,PBC Tool - Normal,Peace of Mind Benefits,Personnel Records,Promotions,Reporting,Reporting - Data Quality Integrity,Site Utilities,Single Cycle Salary Review,Transition Employees,Transactional,Well Being,When Life Changes,Workforce Diversity,Workplace Practices,Your Career __data__ Bob Showalter [EMAIL PROTECTED] 30/12/2003 02:22 PM To: Colin Johnstone/Australia/Contr/[EMAIL PROTECTED] cc: [EMAIL PROTECTED] Subject:Re: Splitting Comma delimited list Colin Johnstone wrote: Bob, The cms makes those changes. It also inserts comments as shown below which aren't in my code and wraps it all in the select tags. A suggestion: Post a message with the input data, the script, and the actual output produced *by that script*. Put the input data under a __DATA__ tag. Leave the cms out of it for now. That way we can run your self-contained script and see what's going on. Heck, you may even find the problem while going through this exercise :~) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: splitting a string
On Dec 10, 2003, at 4:49 PM, Ravi Malghan wrote: Hi: I want to split the string 0.0.0.0.1.10.1.30.1.10.1.30.1 into 4 variables: 0.0.0.0, 1, 10.1.30.1 and 10.1.30.1 any suggestions? yes, get better data. a part of the problem you have is the that you could do this with a regEx my $input = '0.0.0.0.111.10.1.30.1.10.1.30.1 '; my $dot_quad = qr/\d+\.\d+\.\d+\.\d+/; my ($im, $jm , $km, $mm ) = $input =~ /($dot_quad)\.(\d+)\.($dot_quad)\.($dot_quad)/; cf perldoc perlre ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: splitting a string
Ravi Malghan wrote: Hi: I want to split the string 0.0.0.0.1.10.1.30.1.10.1.30.1 into 4 variables: 0.0.0.0, 1, 10.1.30.1 and 10.1.30.1 any suggestions? TIA ravi Here is one approach: #!perl -w use strict $_ = '0.0.0.0.1.10.1.30.1.10.1.30.1'; my @MyWorka = (); @MyWorka = split(/\./, $_); my @MyWorkb = (); my @MyWorkc = (); my @MyWorkd = (); @MyWorkb = splice(@MyWorka,-4,4); @MyWorkc = splice(@MyWorka,-4,4); @MyWorkd = splice(@MyWorka,-1,1); printf %-s, %-s, %-s, %-s\n, join('.', @MyWorka), @MyWorkd, join('.', @MyWorkc), join('.', @MyWorkb); Output: 0.0.0.0, 1, 10.1.30.1, 10.1.30.1 Wags ;) __ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/ ** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: splitting a string
For those of you that are interested, I benchmarked a few of the suggestions. Drieux is by a significant margin the winner speed-wise. What I did was split the scalar into an array and then define the four variables by join()ing the result using an array slice instead of making a new array. Running this code, cutting out the print statements: ### use strict; use warnings; use Benchmark; my $mine = timethese(100, {Mine = sub{ my $string = '0.0.0.0.1.10.1.30.1.10.1.30.1'; my @nums = split(/\./,$string); my $first = join('.',@nums[0..3]); my $second = $nums[4]; my $third = join('.',@nums[5..8]); my $fourth = join('.',@nums[9..12]); },Drieux = sub{ my $input = '0.0.0.0.111.10.1.30.1.10.1.30.1 '; my $dot_quad = qr/\d+\.\d+\.\d+\.\d+/; my ($im, $jm , $km, $mm ) = $input =~ /($dot_quad)\.(\d+)\.($dot_quad)\.($dot_quad)/; },Wags = sub{ $_ = '0.0.0.0.1.10.1.30.1.10.1.30.1'; my @MyWorka = (); @MyWorka = split(/\./, $_); my @MyWorkb = (); my @MyWorkc = (); my @MyWorkd = (); @MyWorkb = splice(@MyWorka,-4,4); @MyWorkc = splice(@MyWorka,-4,4); @MyWorkd = splice(@MyWorka,-1,1); }}); # Gave me this result: Benchmark: timing 100 iterations of Drieux, Mine, Wags... Drieux: 9 wallclock secs ( 8.16 usr + 0.00 sys = 8.16 CPU) @ 122518.99/s (n=100) Mine: 12 wallclock secs (11.76 usr + 0.00 sys = 11.76 CPU) @ 85055.71/s (n=100) Wags: 14 wallclock secs (14.53 usr + 0.00 sys = 14.53 CPU) @ 68818.39/s (n=100) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Splitting OR Regex
R. Joseph Newton wrote: Rob Dixon wrote: my @fields = $string =~ m/\w+=(?:[^]+|\S+)/g; Nice!! blush Thanks! /blush Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Splitting OR Regex
Rob Dixon wrote: my @fields = $string =~ m/\w+=(?:[^]+|\S+)/g; Nice!! Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Splitting OR Regex
On Thu, 30 Oct 2003 23:37:55 -0500, Scott, Joshua wrote: How can I split the data in a line by a single whitespace but also keep portions between quotes together? This is a FAQ: perldoc -q delimit -- Tore Aursand [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Splitting OR Regex
Joshua Scott wrote: How can I split the data in a line by a single whitespace but also keep portions between quotes together? Here is an example of a line of data I'm trying to parse: id=firewall time=2003-10-30 04:15:01 fw=66.74.67.229 pri=5 c=256 m=38 msg=ICMP packet dropped n=63211 src=1.1.1.1 dst=2.2.2.2 What I would like to do is keep the data between the quotes together despite the spaces. This is what I'm expecting to get: Id=firewall Time=2003-10-30 04:15:01 Fw=66.74.67.229 Msg=ICMP packet dropped ...etc How should I go about doing this? What I have currently is a regex that splits out the entire line, but certain fields have changed so my original code doesn't work as well. Hi Joshua. The 'delimited string' FAQ is really for comma-separated values rather than the 'name=value' format you have here. It also supports escaped quotes within quoted strings, which you probably don't have. The regex below should do what you want. HTH, Rob use strict; use warnings; my $string = q{id=firewall time=2003-10-30 04:15:01 fw=66.74.67.229 pri=5 c=256 m=38 msg=ICMP packet dropped n=63211 src=1.1.1.1 dst=2.2.2.2}; my @fields = $string =~ m/\w+=(?:[^]+|\S+)/g; print map $_\n, @fields; *OUTPUT* id=firewall time=2003-10-30 04:15:01 fw=66.74.67.229 pri=5 c=256 m=38 msg=ICMP packet dropped n=63211 src=1.1.1.1 dst=2.2.2.2 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Splitting a path
I may be missing something, but the doc for ENV says: Arrays are implemented in terms of split and join, using $Config::Config{path_sep} as the delimiter. Now if i split this: Foo = C:\foo\bar:D:\foo\bar using: use Env qw(@Foo); print $_\n foreach (@Foo); i guess it would print the whole Foo instead of printing 2 elements C:\foo\bar and D:\foo\bar, because the $Config::Config(path_sep) is not : on windows. -Sharad -Original Message- From: Steve Grazzini [mailto:[EMAIL PROTECTED] Sent: Saturday, August 02, 2003 4:22 PM To: Gupta, Sharad Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Re: Splitting a path On Sat, Aug 02, 2003 at 03:20:14PM -0700, Gupta, Sharad wrote: From: Casey West [mailto:[EMAIL PROTECTED] It was Saturday, August 02, 2003 when Gupta, Sharad took the soap box, saying: : : Hi, : : I have a path which may look like: use Env [EMAIL PROTECTED]; print $_\n foreach @PATH; I love the Perl core. :-) I miscommunicated here, by path i did'nt meant the PATH env variable. By Path i meant any string having some directories seperated by : , say it could be FOO=E:\foo:D:\bar. And yes it can be in the %ENV. Then Casey's suggestion will work for you with just one tiny modification. use Env [EMAIL PROTECTED]; print $_\n foreach @FOO; For more information read http://search.cpan.org/perldoc?Env This is still recommended. ^_^ And it would be great if you could arrange for your mail/news reader to quote the text of the original message rather than appending it unmodified at the end of your response. -- Steve -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]