ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Perl's ability to create lists is quite generous. I can for-example say... (1, 3..5, 20..21, -6, 19). Anything I want.
The second argument to for (foreach) statement, in this form, is simply a list or something that produces a list. Like most things Perl, "there's more than one way to do it." And hence, "there's more than one way to say it."
Tip: Also do not overlook the usefulness of the each() function. In the case of arrays, don't overlook the usefulness of negative indices, where "-1" produces the last element, "-2" the second-from-last, and so on.
Last edited by sundialsvcs; 01-09-2009 at 09:53 AM.
But, I've another question pertaining to perl.
So, I've stdin buffer and parsing that have got array. However I want edit data from stdin buffer and printed what me necessary.
Code:
my $a;
while(<stdin>){
if(..regexp...){
...whatever...
print $a,"data was edited\n";
}
}
Which's way printed all the data from stdin with edited data without change format data on stdin?
for example, after to do...
865
a
134
b
before
870
a
139
b
..so on
I've function which to do changing the data, nevertheless I'm confused about structure data printed.
You can use one of the range operators... (I think you need the three-dot version since the start and end separators are the same.) So, something like this should work:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
while (<>) {
if (m/^--$/...m/^--$/) {
print unless m/^--$/; ## print is just an example;
## push lines into a saved array, if you prefer
}
}
File and output example:
Code:
telemachus ~ $ cat dashes
Hello
Goodbye
foo
bar
--
This
should
print
--
but
not
this
--
This
should
also
print
--
but
not
this
either
Output:
Code:
telemachus ~ $ perl sep dashes
This
should
print
This
should
also
print
thanks, are you really helped me.
Please, suggest me something so as to take necessary printed format.
I've function which's take two arguments, but I should use print func($x,$z); at one time...
what I've got...
Is my function just doing math calculi and return value from $x;
But if I've such as file format me need saved current format file in stdout.
some file...
Code:
232 - a 500
500 - b 599
599 - c 1000
etc...
So, how to printed every two value from foreatch together on one line?
Ok, I'm not sure that I understand what you want, but here's what I have. Your datafile looks like this:
Code:
232 - a 500
500 - b 599
599 - c 1000
0 1 2 3 # These numbers are to count elements, not part of the file
You want the two numbers, so what you really want is the 0th and the third element, since you start with the first number and count from there. (See the numbers I added above.) So what I think you want to do here is split $_ (the current line) on white space, since white space separates the elements. Since both $_ and white space are defaults for split, you can write this very briefly:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
while (<>) {
my ( $number1, $number2 ) = (split)[0, 3]; # Split $_ on whitespace and assign the 0th and 3rd elements
print "$number1, $number2\n"; # Print those two numbers; this is just an example, you could
# easily pass them to a subroutine instead
}
Ok, I'm not sure that I understand what you want, but here's what I have. Your datafile looks like this:
Are you worse understand me because my english skill is very poor?
right, however if I've two logical conditions, which's way I can took that?
Just I need printed that part by part
Code:
if(..regex...){
my ( $number1, $number2 ) = (split)[0, 3];
print "$number1, $number2\n";
}
if (m/^--$/...m/^--$/) {
print unless m/^--$/;
}
before
Code:
232 - a 500
aaa
500 - b 599
bbb
599 - c 1000
ccc
after doing it
Code:
231 501
aaa
501 600
bbb
600 1001
ccc
I mean what I should use array, but I don't know how to do it in perl.
Don't worry pertaining to much more value is numbers, I knew how to do it
___
thank in advance!
Yes, I'm having a hard time understanding your English. I apologize, but I'm more confused now. The datafile you have this time is different. Now it has two different kinds of lines.
Code:
232 - a 500 # Type 1 - numbers, a dash and a letter
aaa # Type 2 - 3 of the letter in the previous line?
500 - b 599 # Type 1 again
bbb # Type 2 again
You seem to want to leave type 2 alone, to remove the numbers from type 1 and then to add 1 to each of those? Your second example seems to want that (except that 231 should be 233?), but I don't know.
I can show you how to put items into an array, but I don't really see what you want here. Here is one way to do what I think you may want. I capture the two numbers, create an anonymous array reference for the pair and then push that reference onto the @numbers array. Then this prints the numbers + 1 out:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
my @numbers;
while (<>) {
my ( $number1, $number2 ) = (split)[0, 3];
push @numbers, [ $number1, $number2 ];
}
foreach my $aref (@numbers) {
printf "%d %d\n", $aref->[0] + 1, $aref->[1] + 1;
}
Output from the datafile I had originally (without the letter lines):
I'm wrote about that in last proposal on my previous post.
Quote:
You seem to want to leave type 2 alone, to remove the numbers from type 1 and then to add 1 to each of those? Your second example seems to want that (except that 231 should be 233?), but I don't know.
Yes, I did a mistakes.
very thanks, I solved if by oneself. Just, I'm forgot about 'else' operator...
if(..regex...){
...
}else{
...
}
__
best regards.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.