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 07-03-2009, 03:29 AM   #1
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Rep: Reputation: 15
need perl suggestion


hi all,
I'm writing this two code snippets and need suggestion about that.
1) So, how I can print line wich contained into $x?
Code:
#!/usr/bin/perl
use diagnostics;
use strict;
my @file= <DATA>;
my $str = join( '', @file);
my $x = '/dev/sda1';
if($str =~  /Mounted\s+on(.*?)/sgi ){
my @s = join '', $1;
print   grep(/$x/,@s);
}
__DATA__
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda3            487730816 484563176   3167640 100% /
/dev/sda1               248895    138484     97561  59% /boot
/dev/sdb1            488252928 314127712 174125216  65% /mnt/s
/dev/sdc1             15674912  14776312    898600  95% /media/disk-2
2) and there...
If I to enroll values to 'a' and 'b' and in the next I need use foreach and evaluate either value from either key. When that set key 'a' it should be evaluate 1 and second if that need. Second and other values should be use only if use of first was destroyed. Actually I need perl-tecnics how that make.
Code:
%h = (
a=>1,
b=>2,
c=>3,
d=>4,
);
my $x;
%h = map {$_ => [$h{$_}]} keys %h;
push @{$h{a}}, 2; 
push @{$h{b}}, 4;
 
Old 07-03-2009, 05:17 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Try out this code section for ur first problem

my $x = '/dev/sda1';
while (<DATA>)
{
chomp;
my ($fs, $bl, $us, $a, $up, $m) = split (/\s+/, $_);
if ($fs eq $x)
{
print "Filesystem=$fs,1K-blocks=$bl,Used=$us,Available=$a,Use=$up,Mounted=$m\n";
}
}


Please clarify your second question

Last edited by PMP; 07-03-2009 at 05:19 AM.
 
Old 07-03-2009, 06:12 AM   #3
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Please clarify your second question
ok, what I do...
Code:
my $z;
foreach(%h){
my $w =$_;
foreach(@{$h{$w}}){
my @a;
$z=$_;
nextvalue:
if(grep(/$h{$z}/i,@a)){
}else{
if(destroyer($z)){
...work..
}else{
foreach (@{$h{$_}}){
$lz=$_; 
	if($lz ne $z){
	goto nextvalue;
	}
}
}
push(@a,"$w\n"); # so as not get it twice
}

}
}
sub destroyer(){
if(...smth...){
return 0;
}else{
return 1;
}
}
unfortunaly, the some keys can use twice several values and then I've got not true result.

Last edited by umbrella2; 07-03-2009 at 06:18 AM.
 
Old 07-03-2009, 06:38 AM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Can everyone please both put code into [code][/code] tags and indent the code properly? It's very hard to read something longer than a few lines with zero indentation.

@Umbrella - if you know that the keys are not unique, then a hash may not be your best option. Hash keys must be unique. You probably need to use a different data structure. Edit: Actually, now that I look at your first post again, I realize that I have nearly no idea what you are actually trying to do. Can you please explain it more clearly?

Last edited by Telemachos; 07-03-2009 at 06:41 AM.
 
Old 07-03-2009, 09:52 AM   #5
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
I'm gave code snippet which kind of works, but not for everyone key. So, if $z missed, then func destroyer return zero and main while should go to the next value in 'a'(which's not equal other pasted values in there) and so on... until destroyer returned 1. And if destroyer returned 1 in first value that's good and it enough for 'a'. Because I need do work everyone key, but if value in that key was true.

Last edited by umbrella2; 07-03-2009 at 09:57 AM.
 
Old 07-05-2009, 09:04 AM   #6
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
any ideas?

and could you help me with that...
Code:
my $ip ='10.10.10.10'
exec qq( /usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr=$ip/g' /etc/pf.conf) ;
system q(/usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr="$ip"/g' /etc/pf.conf);
I tried different ways todo that, but unfortunaly it doesn't work.
 
  


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: give me suggestion quantt Programming 5 05-19-2009 02:12 PM
LXer: Installing Eclipse, the Epic Perl plugin and my first Perl GUI program LXer Syndicated Linux News 0 05-08-2009 06:41 PM
RHEL4u2 perl localtime() call issues (perl & glibc) Brad.Scalio@noaa.gov Linux - Enterprise 3 01-23-2007 09:27 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

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

All times are GMT -5. The time now is 05:48 AM.

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