Chopping strings
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 How should I do to split a long string that the only thing I know is that is a multiple of a previous defined number (for example 26). A graphic example: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz That's four times 26 (104) but I don't (I can't) count every string because they are really long (They can be of thousands of charachters). So what I should do, I think, is a loop that get the first 26 charachters and print them in a new file, after that evaluates if there is more string to get, and so on, but I am not able to see how it works... :-/ Give me some advice, please (the command I should use, or group of commands, etc...) Thanks in advance for your help and patience. Josep Sànchez ~ [papapep] -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.1 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE+U2pP2vx52x0kyz4RArgbAKCwR88CWEfO0I8eePfRL42XsoTKxACgkm5d hJTlHvX22x+LSCazIBDsruc= =qw3e -END PGP SIGNATURE- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Chopping strings
From: papapep [EMAIL PROTECTED] How should I do to split a long string that the only thing I know is that is a multiple of a previous defined number (for example 26). A graphic example: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr stuvwxyzabcdefghijklmnopqrstuvwxyz That's four times 26 (104) but I don't (I can't) count every string because they are really long (They can be of thousands of charachters). So what I should do, I think, is a loop that get the first 26 charachters and print them in a new file, after that evaluates if there is more string to get, and so on, but I am not able to see how it works... :-/ while ($long_string ne '') { my $chunk = substr( $long_string, 0, 26); substr( $long_string, 0, 26) = ''; # yes I know this looks strange. Forget Computer Science, it does # what it looks like doing. ... } Jenda = [EMAIL PROTECTED] === http://Jenda.Krynicky.cz = When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Chopping strings
Papapep wrote: How should I do to split a long string that the only thing I know is that is a multiple of a previous defined number (for example 26). A graphic example: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz That's four times 26 (104) but I don't (I can't) count every string because they are really long (They can be of thousands of charachters). So what I should do, I think, is a loop that get the first 26 charachters and print them in a new file, after that evaluates if there is more string to get, and so on, but I am not able to see how it works... :-/ Give me some advice, please (the command I should use, or group of commands, etc...) Here are a couple of ways to do it: my $length = 26; my $string = 'abcdefghijklmnopqrstuvwxyz' x 4; Split on arbitrary length using match operator: for my $line ( $string =~ /.{1,$length}/sg ) { # $line contains 1 to $length characters } Split on arbitrary length using substr (C style): for ( my $index = 0; my $line = substr $string, $index, $length; $index += $length ) { # $line contains 1 to $length characters } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Chopping strings
Jenda Krynicky wrote: while ($long_string ne '') { my $chunk = substr( $long_string, 0, 26); substr( $long_string, 0, 26) = ''; Or just: my $chunk = substr( $long_string, 0, 26, '' ); But that is inefficient because you modify the beginning of the string on each iteration. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Chopping strings
John W. Krahn wrote: Papapep wrote: How should I do to split a long string that the only thing I know is that is a multiple of a previous defined number (for example 26). A graphic example: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx yzabcdefghijklmnopqrstuvwxyz That's four times 26 (104) but I don't (I can't) count every string because they are really long (They can be of thousands of charachters). So what I should do, I think, is a loop that get the first 26 charachters and print them in a new file, after that evaluates if there is more string to get, and so on, but I am not able to see how it works... :-/ Give me some advice, please (the command I should use, or group of commands, etc...) You might look at sysopen, sysread which allows you to read in max blocks of data and process that data. It would look something like: sysopen(FILEIN,$filein,0) || die unable to open file $filein: $!; bindmode(FILEIN); my $rcdbuf = ; my $rcdcnt = 0; my $rcdmax = 26; my $rcdcntrtn = 0; my @MyWorka = (); while ( 1 ) { sysseek(FILEIN,$rcdmax*$rcdcnt,0); $rcdcntrtn = sysread(FILEIN,$rcdbuf,$rcdmax); if ( defined $rcdcntrtn ) { @MyWorka = unpack(a26,$rcdbuf); $rcdcnt++; # could write your new file out here or do what you want. # if there can be less an x multiples, then you might need to check the length }else { last ; } } # end of while close(FILEIN); A different approach. ps If there can be linefeeds or carriage returns, then you will have to approach it different also. 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]
Re: Chopping strings
David --- Senior Programmer Analyst --- Wgo Wagner wrote: You might look at sysopen, sysread which allows you to read in max blocks of data and process that data. It would look something like: sysopen(FILEIN,$filein,0) || die unable to open file $filein: $!; bindmode(FILEIN); $ perldoc -f bindmode No documentation for perl function `bindmode' found binmode(FILEIN); my $rcdbuf = ; my $rcdcnt = 0; my $rcdmax = 26; my $rcdcntrtn = 0; my @MyWorka = (); while ( 1 ) { sysseek(FILEIN,$rcdmax*$rcdcnt,0); Using sysseek is redundant as sysread will set the file pointer to the end of the chunk of data it just read. $rcdcntrtn = sysread(FILEIN,$rcdbuf,$rcdmax); if ( defined $rcdcntrtn ) { sysread() could return less then the requested bytes so this may not do what you want it to do (how do you know the buffer is full?) @MyWorka = unpack(a26,$rcdbuf); You already (hopefully) have 26 bytes in $rcdbuf so why are you using unpack? The 'a' format of unpack will read the data in $rcdbuf up to the first \0 (NULL) character (like a C string) so this may not do what you want. $rcdcnt++; # could write your new file out here or do what you want. # if there can be less an x multiples, then you might need to check the length }else { last ; } } # end of while close(FILEIN); A different approach. ps If there can be linefeeds or carriage returns, then you will have to approach it different also. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]