LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2009, 02:51 AM   #1
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Rep: Reputation: 15
perl question


g'day everyone,
I've this code, but take a problem with do compare $a variable and zero or fourth arguments from array.
Code:
 	split /(|)/;
# 	print "$_[0]\n$_[4]". "\n";
foreach my $a (@_) {
    own_func($a);
}
Tell me please, how took only zero and fourth arguments from array, not either?
 
Old 01-09-2009, 09:50 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
One simple way might be to use a list...

Code:
foreach my $foo (1, 4) {
  bar($foo);
}
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.
 
Old 01-09-2009, 12:34 PM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by umbrella2 View Post
g'day everyone,
I've this code, but take a problem with do compare $a variable and zero or fourth arguments from array.
Code:
 	split /(|)/;
# 	print "$_[0]\n$_[4]". "\n";
foreach my $a (@_) {
    own_func($a);
}
Tell me please, how took only zero and fourth arguments from array, not either?
I don't understand your question. Do you want to access the 0th and 4th index of the split? If so, you could use a slice like this:
Code:
 my ( $zero, $four ) = ( split /\|/, $_ )[0, 4];
But is this what you want? Or, do you want to compare those two items to each other?

By the way, as a general rule, you should avoid naming variables $a or $b, since Perl uses those two already for sort.
 
Old 01-10-2009, 06:35 AM   #4
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
thanks, I took it of than way.
Code:
foreach $a ($_[0], $_[4]) {
	#print $a,"\n";
		}
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.
 
Old 01-10-2009, 07:48 AM   #5
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
Quote:
Originally Posted by Telemachos View Post
I don't understand your question. Do you want to access the 0th and 4th index of the split? If so, you could use a slice like this:
Code:
 my ( $zero, $four ) = ( split /\|/, $_ )[0, 4];
But is this what you want? Or, do you want to compare those two items to each other?

By the way, as a general rule, you should avoid naming variables $a or $b, since Perl uses those two already for sort.
I also didn't get the question at first but I think he/she wanted to compare a variable named $a, with the 0th or 4th entry in an array.

(I think he/she is using a language translator that's why it is not quite clear)
 
Old 01-11-2009, 06:25 AM   #6
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
So, how to catch data between lines which has '--'.
for example...
Quote:
--
...what I need...
--
I can took only line with '--', bat couldn't get value find out between lines with '--'.
 
Old 01-11-2009, 08:07 AM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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
 
Old 01-12-2009, 05:21 AM   #8
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
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...
Code:
while(<>){
foreatch $z ($_[0],$_[4]){
print func($x,$z)"\n";
}
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?
 
Old 01-12-2009, 06:46 AM   #9
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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
}
If I run that as ./getnums datafile I get this
Code:
hektor ~ $ ./getnums datafile 
232, 500
500, 599
599, 1000
I hope this helps.
 
Old 01-12-2009, 08:29 AM   #10
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
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!

Last edited by umbrella2; 01-12-2009 at 08:32 AM.
 
Old 01-12-2009, 10:07 AM   #11
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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):
Code:
hektor ~ $ ./getnums datafile 
233 501
501 600
600 1001
More generally, I think you need to check out the push function for getting items onto an array.

Last edited by Telemachos; 01-12-2009 at 10:10 AM.
 
Old 01-12-2009, 04:08 PM   #12
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
The datafile you have this time is different
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Question for perl hackers. linuxlover1 Programming 1 06-27-2006 06:56 PM
reinstall perl question.. (urpme perl) rjcrews Mandriva 2 01-28-2006 06:19 PM
Perl question RHrulz Programming 2 07-23-2004 01:15 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration