Miha Radej wrote:
> hi!
>
> i'm writing a script which does some file copying and similar and have
> stumbled upon two problems i thought will be easy but i didn't manage to
> solve them on my own.
> firstly, i have a file consisting of lines which contain certainf filter
> instructions. like so:
>
> last f:\centrale\n300\d*.asm 2 c:\n300
>
> how do i split up the line into its components if the whitespaces are
> one or more spaces and/or one or more tabs? i need to get for example an
> array which contains the elements, like so:
> @array[0] = "last";
> @array[1] = "f:\\centrale\\n300\\d*.asm";
> @array[2] = "2";
> @array[3] = "c:\\n300";
use strict;
$_ = 'last f:\centrale\n300\d*.asm 2 c:\n300' . "\n";
my @array = split ' ', $_;
foreach (@array) {
print "'$_'\n";
}
__END__
Yields:
'last'
'f:\centrale\n300\d*.asm'
'2'
'c:\n300'
> and the other problem is that when copying certain text files i need to
> open them, see if any of the lines begins with 8, 16 or 24 spaces and
> when i need to write the data in a new file, i need to convert those
> spaces into tabs (8 spaces = 1 tab). is there a simple way to do this
> within my script?
use strict;
use Text::Tabs;
my @lines_without_tabs = ();
foreach (1, 6, 7, 8, 15, 16, 17, 23, 24, 25, 31, 32, 33) {
push @lines_without_tabs,
sprintf "A line with %u spaces here '%s'\n", $_, ' ' x $_;
}
print "lines_without_tabs:\n";
dumpit ([EMAIL PROTECTED]); # dumpit routine not included here
my @lines_with_tabs = unexpand (@lines_without_tabs);
print "\nlines_with_tabs:\n";
dumpit ([EMAIL PROTECTED]);
__END__
Yields:
lines_without_tabs:
0000 41 20 6C 69 6E 65 20 77 69 74 68 20 31 20 73 70 A line with 1 sp
0010 61 63 65 73 20 68 65 72 65 20 27 20 27 0A 41 20 aces here ' '.A
0020 6C 69 6E 65 20 77 69 74 68 20 36 20 73 70 61 63 line with 6 spac
0030 65 73 20 68 65 72 65 20 27 20 20 20 20 20 20 27 es here ' '
0040 0A 41 20 6C 69 6E 65 20 77 69 74 68 20 37 20 73 .A line with 7 s
0050 70 61 63 65 73 20 68 65 72 65 20 27 20 20 20 20 paces here '
0060 20 20 20 27 0A 41 20 6C 69 6E 65 20 77 69 74 68 '.A line with
0070 20 38 20 73 70 61 63 65 73 20 68 65 72 65 20 27 8 spaces here '
0080 20 20 20 20 20 20 20 20 27 0A 41 20 6C 69 6E 65 '.A line
0090 20 77 69 74 68 20 31 35 20 73 70 61 63 65 73 20 with 15 spaces
00A0 68 65 72 65 20 27 20 20 20 20 20 20 20 20 20 20 here '
00B0 20 20 20 20 20 27 0A 41 20 6C 69 6E 65 20 77 69 '.A line wi
00C0 74 68 20 31 36 20 73 70 61 63 65 73 20 68 65 72 th 16 spaces her
00D0 65 20 27 20 20 20 20 20 20 20 20 20 20 20 20 20 e '
00E0 20 20 20 27 0A 41 20 6C 69 6E 65 20 77 69 74 68 '.A line with
00F0 20 31 37 20 73 70 61 63 65 73 20 68 65 72 65 20 17 spaces here
0100 27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 '
0110 20 20 27 0A 41 20 6C 69 6E 65 20 77 69 74 68 20 '.A line with
0120 32 33 20 73 70 61 63 65 73 20 68 65 72 65 20 27 23 spaces here '
0130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0140 20 20 20 20 20 20 20 27 0A 41 20 6C 69 6E 65 20 '.A line
0150 77 69 74 68 20 32 34 20 73 70 61 63 65 73 20 68 with 24 spaces h
0160 65 72 65 20 27 20 20 20 20 20 20 20 20 20 20 20 ere '
0170 20 20 20 20 20 20 20 20 20 20 20 20 20 27 0A 41 '.A
0180 20 6C 69 6E 65 20 77 69 74 68 20 32 35 20 73 70 line with 25 sp
0190 61 63 65 73 20 68 65 72 65 20 27 20 20 20 20 20 aces here '
01A0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
01B0 20 20 20 20 27 0A 41 20 6C 69 6E 65 20 77 69 74 '.A line wit
01C0 68 20 33 31 20 73 70 61 63 65 73 20 68 65 72 65 h 31 spaces here
01D0 20 27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 '
01E0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
01F0 20 27 0A 41 20 6C 69 6E 65 20 77 69 74 68 20 33 '.A line with 3
0200 32 20 73 70 61 63 65 73 20 68 65 72 65 20 27 20 2 spaces here '
0210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 27 '
0230 0A 41 20 6C 69 6E 65 20 77 69 74 68 20 33 33 20 .A line with 33
0240 73 70 61 63 65 73 20 68 65 72 65 20 27 20 20 20 spaces here '
0250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 27 0A '.
lines_with_tabs:
0000 41 20 6C 69 6E 65 20 77 69 74 68 20 31 20 73 70 A line with 1 sp
0010 61 63 65 73 20 68 65 72 65 20 27 20 27 0A 41 20 aces here ' '.A
0020 6C 69 6E 65 20 77 69 74 68 20 36 20 73 70 61 63 line with 6 spac
0030 65 73 20 68 65 72 65 20 27 09 20 27 0A 41 20 6C es here '. '.A l
0040 69 6E 65 20 77 69 74 68 20 37 20 73 70 61 63 65 ine with 7 space
0050 73 20 68 65 72 65 20 27 09 20 20 27 0A 41 20 6C s here '. '.A l
0060 69 6E 65 20 77 69 74 68 20 38 20 73 70 61 63 65 ine with 8 space
0070 73 20 68 65 72 65 20 27 09 20 20 20 27 0A 41 20 s here '. '.A
0080 6C 69 6E 65 20 77 69 74 68 20 31 35 20 73 70 61 line with 15 spa
0090 63 65 73 20 68 65 72 65 20 27 09 09 20 20 20 27 ces here '.. '
00A0 0A 41 20 6C 69 6E 65 20 77 69 74 68 20 31 36 20 .A line with 16
00B0 73 70 61 63 65 73 20 68 65 72 65 20 27 09 09 20 spaces here '..
00C0 20 20 20 27 0A 41 20 6C 69 6E 65 20 77 69 74 68 '.A line with
00D0 20 31 37 20 73 70 61 63 65 73 20 68 65 72 65 20 17 spaces here
00E0 27 09 09 20 20 20 20 20 27 0A 41 20 6C 69 6E 65 '.. '.A line
00F0 20 77 69 74 68 20 32 33 20 73 70 61 63 65 73 20 with 23 spaces
0100 68 65 72 65 20 27 09 09 09 20 20 20 27 0A 41 20 here '... '.A
0110 6C 69 6E 65 20 77 69 74 68 20 32 34 20 73 70 61 line with 24 spa
0120 63 65 73 20 68 65 72 65 20 27 09 09 09 20 20 20 ces here '...
0130 20 27 0A 41 20 6C 69 6E 65 20 77 69 74 68 20 32 '.A line with 2
0140 35 20 73 70 61 63 65 73 20 68 65 72 65 20 27 09 5 spaces here '.
0150 09 09 20 20 20 20 20 27 0A 41 20 6C 69 6E 65 20 .. '.A line
0160 77 69 74 68 20 33 31 20 73 70 61 63 65 73 20 68 with 31 spaces h
0170 65 72 65 20 27 09 09 09 09 20 20 20 27 0A 41 20 ere '.... '.A
0180 6C 69 6E 65 20 77 69 74 68 20 33 32 20 73 70 61 line with 32 spa
0190 63 65 73 20 68 65 72 65 20 27 09 09 09 09 20 20 ces here '....
01A0 20 20 27 0A 41 20 6C 69 6E 65 20 77 69 74 68 20 '.A line with
01B0 33 33 20 73 70 61 63 65 73 20 68 65 72 65 20 27 33 spaces here '
01C0 09 09 09 09 20 20 20 20 20 27 0A .... '.
> oh, another thing. i needed a function to count the length of a given
> string. i wrote my own function which does this but is there a built-in
> function i can use instead? searching through the help i have available
> i didn't found anything of much help.
my $len = length $string;
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs